开发者

jquery code explanation

I have this code and I need an explanation on what it does:

function delaymethod(settings) {
开发者_JAVA百科    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);

} 


The code sets a periodic timer (parent.setInterval) that fires every 200 ms. Whenever the timer fires:

  1. It checks settings.condition() and if it is fulfilled, it stops the timer and calls a success() function.

  2. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜