ClearInterval只能工作一次

时间:2015-03-12 23:41:23

标签: jquery jquery-plugins

有人可以帮我这个代码吗?间隔不会停止,我看不出我做错了什么。当我点击链接间隔时第一次停止,第二次它们通过所有三个。

if(location_type == 'friends'){ 
    GetReactionCount(location_type,'0',limits);
    intervals1 = setInterval(function() {
    GetReactionCount(location_type,'0',limits);
    }, 10000);
    return
        }else
    if(location_type != 'friends'){ clearInterval(intervals1); }    

    if(location_type == 'neighborhood'){
         GetReactionCount(location_type,postcode,limits);
         intervals2 = setInterval(function() {
        GetReactionCount(location_type,postcode,limits);
         }, 10000);
        return
        }else
    if(location_type != 'neighborhood'){clearInterval(intervals2); }

    if(location_type == 'city'){
         GetReactionCount(location_type,stad,limits);
         intervals3 = setInterval(function() {
        GetReactionCount(location_type,stad,limits);
         }, 10000);
        return
        }else
    if(location_type != 'city'){clearInterval(intervals3); }

1 个答案:

答案 0 :(得分:0)

您有三个可能的时间间隔,存储在intervals1intervals2intervals3中。每次调用此代码时,只停止其中一个间隔,因此如果第二次调用此代码,其他两个间隔中的一个正在运行,那么它将继续运行,您将启动另一个,因此您将有两个或者在此时间点运行更多的间隔。

如果只想运行一个间隔,则只在一个变量中存储一个间隔,并在开始一个新间隔之前停止该间隔。或者,如果你需要所有三个区间变量,那么在开始一个新变量之前,做一个在所有三个变量上调用clearInterval()的简短函数。

相关问题