clearInterval和范围 [英] clearINterval and scope

查看:138
本文介绍了clearInterval和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些间隔的问题.

Having problems with some intervals here.

基本概念是,我具有进度条功能,每行事物1个,是从后端脚本中获取信息的.我每隔5秒钟对Ajax调用进行一次ping操作.

The basic concpt is that i have a progress bar function, 1 per row of things, being fed info from a back end script. I ping this script every 5 seconds within an interval to an ajax call.

这有效,但是倍数存在问题(即,第二个进度条调用相同的功能),或者当该项目被关闭"(div幻灯片)时,它应该停止.这是一些非常复杂的代码,所以我会尽力提取必要的部分,但是请注意,就目前而言,这可能不会运行.

This works, however there are problems with multiples (ie, a 2nd progress bar calls the same function) or when the item is 'closed' (a div slide) it should stop. This is some very inter-trined code, so i'll try my best to pull out the necessary parts, but note that as it stands, this likely won't run.

完整的步行功能名称已更改,以保护无辜的人:)

a complete walk though- function names have been changed to protect the innocent :)

onlcick,div打开,此启动函数称为:

onlcick, div opens, and this start function is called:

function start(ID){
var request = $.ajax({  
    url: "script?DO=Start&ID="+ID
    ,  type: "GET"              
    ,  cache: false
    ,  dataType: "text"
});
request.done(function(msg) {
    startLoop(ID);      

});
request.fail(function(jqXHR, textStatus) {
    //alert("Request failed: " + textStatus );      
});
}

如您所见,这将调用startLoop fnc,它将继续ping更新信息脚本(getInfo())以获得信息:

as you can see, this calls the startLoop fnc, which continues to ping the update info script (getInfo()) for info:

function startLoop(ID){
var i = setInterval(function(){
    var xstatus = getInfo(ID);
    //other stufff that works       
    if(xstatus=="done"){
        clearInterval(i);
    }

}, 5000);
}

这本身可以很好地工作.

This in and of it self works fine.

如果第二次单击事件打开了第二个窗口,则它只是具有一个新的ID并调用相同的功能.

If a 2nd click event opens a 2nd window, it simply has a new ID and calls the same functions.

因此,我有一个i间隔,并且销毁一个似乎可以阻止一切. 我应该将i转换为i.ID之类的对象吗?

Thus, I have one i interval, and destroying one seems to stop everything. Should i convert i to be an object, like i.ID ?

另外,当停止时,我有一个onclick = stop()调用,并且从循环之外,我似乎无法停止它.

Also, when stopped, I have an onclick =stop() call, and from outside the loop, I can't seem to stop it.

我如何重新安排它以使其正常工作?

How do i re-arrange this to make it work?

它应该支持1个或多个同时出现的项目的开始,停止和自动更新.

it should support start, stop, and autoupdating, for 1 or many simultaneous items.

推荐答案

为什么不使用setTimeout而不是clearInterval,因为您已经将杀死间隔的责任放在了回调中:

Why not use setTimeout instead of clearInterval, since you're already putting the responsibility of killing the interval inside its callback:

// using this approach might require changing the name of this function
function startLoop(ID) { 
   setTimeout(function(){
        var xstatus = getInfo(ID);

        //other stufff that works       
        if(xstatus!=="done"){
            startLoop(ID); // do it all over again if it's not complete
        }

    }, 5000);
}

这将给您相同的行为,但有一些额外的好处.首先,这不会导致您的其他工作在完成时被杀死.另一个是,此操作将异步运行,因此,如果请求开始花费5秒钟以上才能完成,则超时将在返回后5秒钟内再次ping通,而不是每5秒钟盲目地触发一次,无论是否请求已完成.

This will give you the same behavior, but with a couple of added bonuses. For one, this shouldn't result in your other jobs being killed when one is complete. The other is that this will run asynchronously, so if the requests start taking more than 5 seconds to complete, the timeout won't ping it again for 5 seconds after it returns, as opposed to blindly firing every 5 seconds, whether or not the request has been completed.

这篇关于clearInterval和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆