Skip to content

Instantly share code, notes, and snippets.

@greyd
Created May 27, 2012 11:37
Show Gist options
  • Save greyd/2808511 to your computer and use it in GitHub Desktop.
Save greyd/2808511 to your computer and use it in GitHub Desktop.
ДЗ 7
function waitsForMaker( timestep, timeout ) {
var timestep = timestep || 100,
timeout = timeout || 5000;
return function( predicat, callback, errorback ) {
var stepCount = Math.ceil(timeout / timestep),
step = 0,
interval;
interval = setInterval( function() {
step++;
if (predicat()) {
clearInterval(interval);
callback();
} else if (step >= stepCount) {
clearInterval(interval);
errorback();
}
}, timestep);
}
}
var waitsFor = waitsForMaker(50, 5000);
waitsFor(
function(){
return typeof watched.foo == 'function';
},
function() {
alert('callback');
},
function() {
alert('errorback');
});
watched = {};
setTimeout( function() {
watched.foo = function() {};
}, 3000);
//смотрим в консоли что происходит с переменной
setInterval( function() {
console.log(typeof watched.foo == 'function');
},500);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment