javascript IE 9 上的 setTimeout() 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14704865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:21:34  来源:igfitidea点击:

setTimeout() problems on IE 9

javascriptobjectinternet-explorer-9settimeout

提问by art.mu

I have a simple js structure like this :

我有一个简单的 js 结构,如下所示:

var Waiting = (function () {

    function Waiting() {
        this.timer;
    }

    Waiting.prototype.show = function () {
        var self = this;

        clearTimeout( self.timer );
        self.timer = setTimeout( function(){ self.hideLogo(); },3000);
    }

     Waiting.prototype.hideLogo = function () {
         console.log("ok i passed timeout");
     };

     return Waiting;
})();

As expected, I get the "ok i passed timeout" log on every browser the first time I execute the show function (which called the hideLogo one). The problem appears in IE9 when I called for the second time the show function. This time, the hideLogo function is never called (log never appears in IE console). I tried a lot of things without any success.

正如预期的那样,当我第一次执行 show 函数(称为 hideLogo 函数)时,我在每个浏览器上都得到了“ok i pass timeout”日志。当我第二次调用 show 函数时,问题出现在 IE9 中。这一次,从不调用 hideLogo 函数(日志永远不会出现在 IE 控制台中)。我尝试了很多事情都没有成功。

If anyone as an idea...

如果有人作为一个想法......

回答by Elias Van Ootegem

When you're using setTimeout, the function that is being called looses the context: in other words thisdoesn't post to the instance on which the method is called anymore. You're using selfto cancel this issue out, but selfis, itself, an iffyword (as in reserved keyword). Perhaps use that, and use an IIFE in the setTimeoutcall:

当您使用时setTimeout,被调用的函数会丢失上下文:换句话说this,不再发布到调用该方法的实例。你self用来取消这个问题,但self它本身是一个不确定的词(如保留关键字)。也许使用that,并在setTimeout调用中使用 IIFE :

this.timer = setTimeout((function (that)
{
    return function()
    {
        clearTimeout(that.timer);//perhaps clear timeout here?
        that.hideLogo.apply(that,[]);//double dutch, the apply _shouldn't_ be required
    };
}(this)), 3000);

At first glance, that's the only thing I can see that might be the issue with your code: the clearTimeoutcall shouldn't be an issue, but I like to call it at the end of the timeout itself, and the selfambiguity thing. Let me know if this changes anything for you!

乍一看,这是我唯一能看到的可能是您的代码的问题:clearTimeout调用不应该是一个问题,但我喜欢在超时本身结束时调用它,并且是self模棱两可的事情。如果这对您有任何改变,请告诉我!

回答by HMR

I am not really sure how you'd call show the second time with the code provided, maybe you create a new Waiting()?

我不确定您如何使用提供的代码第二次调用 show ,也许您创建了一个新的 Waiting()?

Here is what worked for IE8

这是适用于 IE8 的内容

var Waiting=(function () {

    function Waiting() {
        this.timer;
    }

    Waiting.prototype.show = function () {
        var self = this;
        console.log("will clear pref timeout");
        clearTimeout( self.timer );
        self.timer = setTimeout( 
          function(){ 
            self.hideLogo(); 
           },30);
    }

     Waiting.prototype.hideLogo = function () {
         console.log("ok i passed timeout");
     };
     return new Waiting();
})();
// shows only one time
Waiting.show();
Waiting.show();
// next one will show because it lets the prefious one
// finish without clearing the pref timeout.
setTimeout(function(){
Waiting.show();
},1000);

回答by Cain

Try:

尝试:

setTimeout( function(){
    clearTimeout( that.timer );
    that.hideLogo();
},3000);

Worked for me on IE and Chrome. IE is very behind on everything.

在 IE 和 Chrome 上对我来说有效。IE 在所有方面都非常落后。