【问题标题】:How to create a function setInterval with clearInterval inside it?如何在其中创建一个带有 clearInterval 的函数 setInterval?
【发布时间】:2015-02-16 21:06:50
【问题描述】:

我有一个应用程序必须在某些事件后将用户发送到主页。为此,我使用了这段运行良好的代码:

var waitime = 1000;
var handle=setInterval(function () {  
        $('.wrapper').html(divResp);
        $('body').append(js);
        clearInterval(handle);   
    }, waitime);

但我试图创建一个要调用的函数,而不是每次都复制代码。所以,经过一些研究setInterval and how to use clearIntervalclearInterval outside of method containing setInterval 我已经创建了这个:

function refreshToHomePage3(handle,waitime){
     return setInterval(function () {  
                    $('.wrapper').html(divResp);
                    $('body').append(js);
                    clearInterval(handle);   
                  }, waitime);
}    

问题是在调用函数时,像这样:

var refreshIntervalId=refreshToHomePage3(refreshIntervalId,waitime);

我有一个无限循环。我已经使用 setTimeout 而不是 setInterval 解决了这个问题,并且函数变成了这样:

function refreshToHomePage2(waitime){
     setTimeout(function () {  
           $('.wrapper').html(divResposta);
           $('body').append(js); 
     }, waitime);
} 

但我想知道如何使用 setInterval 和 clearInterval 解决问题。有什么想法吗?

【问题讨论】:

    标签: javascript setinterval clearinterval


    【解决方案1】:

    您在代码第一次运行后清除间隔。所以你所做的正是setTimeout 所做的。你需要setTimeout,它在等待waitTime之后只运行一次。

    function refreshToHomePage(handle, waitime) {
        setTimeout(function() {
            $('.wrapper').html(divResp);
            $('body').append(js);
            clearInterval(handle);
        }, waitime);
    }
    

    【讨论】:

    • 您没有分配 setTimeout 来处理。如何清除Interval?
    • 是的。那为什么它在函数内部呢? :)
    【解决方案2】:

    setTimeout 在这里是首选。但是你可以像这样使用setInterval..

    function refreshToHomePage3(handle,waitime){
         handle = setInterval(function () {  
                        $('.wrapper').html(divResp);
                        $('body').append(js);
                        clearInterval(handle);   
                      }, waitime);
         return handle;
    }   
    

    其实不需要给函数传入句柄变量。

    function refreshToHomePage3(waitime){
         var handle = setInterval(function () {  
                        alert("called after waitime");
                        clearInterval(handle);   
                      }, waitime);
         return handle;
    }   
    
    var handle = refreshToHomePage3(5000);

    【讨论】:

    • 这就是 OP 在 refreshToHomePage3 中所做的事情
    • 我不这么认为。在他的示例中,他没有设置句柄变量。 @AmitJoki
    • 首先我猜到了。但后来变了。我不能这么说……抱歉……@AmitJoki
    • 您只是将 setInterval 返回的内容分配给一个变量,然后返回它。 OP 所做的也一样,但没有使用变量
    • 这不是我改变的东西。 OP 使用 clearInterval(handle) 而不为任何东西分配句柄。它不清除间隔。我首先将handle 设置为clearInterval reference,以便clearInterval(handle) 工作..
    【解决方案3】:

    如果您希望您的代码在等待时间之后只执行一次,setInterval 不适合这项工作,但setTimeout 是。

    setInterval 将每 n 秒执行一次您的代码,直到您执行 clearInterval。但是,setTimeout 将在 n 秒后执行您的代码 一次,因此是解决您问题的正确方法。

    不要试图使 setInterval 不是它的东西 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 2021-12-15
      • 2020-01-12
      • 1970-01-01
      • 2011-08-24
      相关资源
      最近更新 更多