Javascript:如何清除非全局(封闭)setTimeout?

发布于 2024-11-05 09:28:35 字数 436 浏览 8 评论 0原文

我正在努力成为一个好公民,并尽可能远离全球范围。有没有办法访问不在全局范围内的 setTimeout 变量?

那么,在这个例子中,人们将如何取消“计时器”?

myObject.timedAction = (function(){
    var timer;
        return function(){
            // do stuff

            // then wait & repeat       
            timer = setTimeout(myObject.timedAction,1000);
        };
})();

我尝试了 clearTimeout(myObject.timedAction.timer,1000); (没有成功),但不确定还可以尝试什么。

I'm trying to be a good citizen and keep as much out of the global scope as possible. Is there a way to access setTimeout variables that are not in the global scope?

So that, in this example how would someone cancel 'timer'?

myObject.timedAction = (function(){
    var timer;
        return function(){
            // do stuff

            // then wait & repeat       
            timer = setTimeout(myObject.timedAction,1000);
        };
})();

I've tried clearTimeout(myObject.timedAction.timer,1000); (without success), and not sure what else to try.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等往事风中吹 2024-11-12 09:28:35

除非您有对 timer 的引用,否则您不能这样做,但您没有引用,因为您将其声明为作用域中的变量。您可以执行以下操作:

myObject.timedAction = (function(){
    return function(){
        // do stuff

        // then wait & repeat       
        myObject.timedAction.timer = setTimeout(myObject.timedAction,1000);
    };
})();

clearTimeout(myObject.timedAction.timer);

请注意,上述代码将只允许一个计时器。如果您需要引用多个计时器,则需要对其进行调整。

You can't unless you have a reference to timer, which you don't because you're declaring it as a variable in a scope. You can do something like:

myObject.timedAction = (function(){
    return function(){
        // do stuff

        // then wait & repeat       
        myObject.timedAction.timer = setTimeout(myObject.timedAction,1000);
    };
})();

clearTimeout(myObject.timedAction.timer);

Note that the above code will only ever allow ONE timer. If you need references to more than one timer, it needs to be adjusted.

冷了相思 2024-11-12 09:28:35

重点是内部变量是私有的,外部世界无法访问。所以你必须稍微改变一下你的方法:

myObject.timedAction = (function(){
    var timer;
    var result = function(){
        // do stuff
        // then wait & repeat       
        timer = setTimeout(myObject.timedAction,1000);
    };

    result.cancel = function() {
        clearTimeout(timer);
    };

    return result;
})();

myObject.timedAction();       // start it
myObject.timedAction.cancel() // end it

所以现在只能从闭包内部访问计时器。是的,您可以向函数添加方法,因为 JS 很棒。

The whole point is that the inner variables are private, and inaccessible to the outside world. SO you have to change your approach a bit:

myObject.timedAction = (function(){
    var timer;
    var result = function(){
        // do stuff
        // then wait & repeat       
        timer = setTimeout(myObject.timedAction,1000);
    };

    result.cancel = function() {
        clearTimeout(timer);
    };

    return result;
})();

myObject.timedAction();       // start it
myObject.timedAction.cancel() // end it

So now the timer is only ever accessed from inside the closure. And yes, you can add methods to a function, because JS is awesome.

寻找一个思念的角度 2024-11-12 09:28:35

将计时器句柄放在对象的属性中:

myObject.timedAction = function(){
  // do stuff
  // then wait & repeat
  this.timer = window.setTimeout(function(){ myObject.timedAction(); },1000);
};

请注意,您应该将计时器的调用包装在函数中,以便将其作为对象的方法而不是全局函数进行调用,否则您将无法使用 this 访问您的对象。

现在您可以使用以下命令停止计时器:

window.clearTimeout(myObject.timer);

Put the timer handle in a property in your object:

myObject.timedAction = function(){
  // do stuff
  // then wait & repeat
  this.timer = window.setTimeout(function(){ myObject.timedAction(); },1000);
};

Note that you should wrap the call from the timer in a function, so that it's called as a method of your object instead of as a global function, otherwise you won't be able to access your object using this.

Now you can stop the timer using:

window.clearTimeout(myObject.timer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文