我有这个例子:

var myApp = (function() {

    var inputClick = function() {
        console.log('inputClick');
    };

    var loadRecentTimeout = function()
    {
    window.setTimeout("inputClick()",3000);
    };

    return {
      loadRecentTimeout:loadRecentTimeout,
      inputClick:inputClick
    };


})();

myApp.loadRecentTimeout(); // this returns inputClick() undefined

window.setTimeout("myApp.inputClick();",3000); // this one seems to work , but it calls that method only one time and not every 3 seconds


谁能解释我如何使此代码每3秒调用一次inputClick()方法?

谢谢

最佳答案

您要调用setInterval而不是setTimeout

var eventInterval = window.setInterval(function () {
                        myApp.inputClick();
                    },3000);


您还应该将函数作为函数而不是字符串传递。

如果您需要取消重复活动,可以致电clearInterval

clearInterval(eventInterval)

关于javascript - 如何在JavaScript和模块模式中使用window.setTimeout,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12043325/

10-11 02:04