使用Revealing Module Pattern的setTimeout

时间:2012-01-13 13:23:16

标签: javascript design-patterns

Revealing module pattern的JavaScript中如何使用setTimeout函数?

这是example

HTML:<div id="container1"></div>

JavaScript:

var classA = (function() {
    var i = 0;
    var names = ["a", "b", "c", "d", "e", "f"];
    var callTest = function() {
        for (var n in names) {
            window.setTimeout(function() {
                callTest2(names[n]);
            }, 1000);
        }
    };

    var callTest2 = function(pName) {
        $("#container1").append("In callTest   " + i+++" " + pName + "<br>");
        window.setTimeout(function() {
            callTest2(pName)
        }, 10000)
    };

    return {
        testTheTest: function() {
            callTest();
        }
    }
})();

classA.testTheTest();

框架:jQuery 1.5.2

当我执行输出时是:

In callTest 0 f
In callTest 1 f
In callTest 2 f
In callTest 3 f
In callTest 4 f
In callTest 5 f

而不是:

In callTest 0 a
In callTest 1 b
In callTest 2 c
In callTest 3 d
In callTest 4 e
In callTest 5 f

我错过了什么?我在哪里做错了?

3 个答案:

答案 0 :(得分:2)

我对您的代码进行了一些细微的修改,这意味着它现在可以按照您的意愿运行:

var classA = (function() {
    var i = 0,
        names = ["a", "b", "c", "d", "e", "f"],
        namesLength = names.length,
        callTest = function() {
            window.setTimeout(function() {
                callTest2(0);
            }, 1000);
        },
        callTest2 = function(pIndex) {
            if (pIndex < namesLength) {
                var name = names[pIndex++];
                $("#container1").append("In callTest   " + i+++" " + name + "<br>");
                window.setTimeout(function() {
                    callTest2(pIndex);
                }, 1000);
            }
        };

    return {
        testTheTest: function() {
            callTest();
        }
    }
})();

classA.testTheTest();

这是working example

答案 1 :(得分:2)

导致函数洞察setTimeout不是在那个地方调用,而是在1秒后调用。当它被调用时,n等于最后一个索引。每次调用函数时,你必须使n为全局并递增

var classA = (function() {
    var i = 0;
    var names = ["a", "b", "c", "d", "e", "f"];
    var n = 0;
    var callTest = function() {
        for (var i in names) {
            window.setTimeout(function() {
                callTest2(names[n]);
                n++;
            }, 1000);
        }
    };

    var callTest2 = function(pName) {
        $("#container1").append("In callTest   " + i+++" " + pName + "<br>");
        window.setTimeout(function() {
            callTest2(pName)
        }, 10000)
    };

    return {
        testTheTest: function() {
            callTest();
        }
    }
})();

classA.testTheTest();

答案 2 :(得分:1)

   for (var n in names) {
            window.setTimeout(function() {                
                callTest2(names[n]);
            }, 1000);
        }

上面的代码等同于以下代码。

    callTest2("f");
    callTest2("f");
    callTest2("f");
    callTest2("f");
    callTest2("f");
    callTest2("f");

为什么..?         原因是函数callTest2()在完成一秒后被调用,但在此之前,整个names []数组已经被迭代,并且“f”最后一个字符被传递给函数callTest2。

对于循环,以微秒的非常小的分数进行。即names []数组将在很短的时间内迭代。         最后,最后一个字符仍为“f”,即名称[n]。