Reputation: 27996
I have this code and I need an explanation on what it does:
function delaymethod(settings) {
settings.timeout = settings.timeout || 2000;
var start = new Date();
var id = parent.setInterval(function () {
if (settings.condition()) {
parent.clearInterval(id);
if (settings.success) {
settings.success();
}
}
var now = new Date();
if (now - start > settings.timeout) {
parent.clearInterval(id);
if (settings.fail) {
settings.fail();
} else if (settings.success) {
settings.success();
}
}
}, 200);
}
Upvotes: -2
Views: 240
Reputation: 70701
The code sets a periodic timer (parent.setInterval
) that fires every 200 ms. Whenever the timer fires:
It checks settings.condition()
and if it is fulfilled, it stops the timer and calls a success()
function.
It checks if a timeout has occurred since the timer was originally set (now - start > settings.timeout
) and if so, it stops the timer and calls either fail()
or success()
, whichever is defined.
Upvotes: 1