Javascript 检查 Interval 是否正在运行,反之亦然

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34805296/
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-08-23 16:52:34  来源:igfitidea点击:

Check if an Interval is running and viceversa

javascriptajax

提问by xnanitox

So i have a web app with basic authentication.

所以我有一个带有基本身份验证的网络应用程序。

When im logged in, an Interval is set:

当我登录时,设置了一个间隔:

$("#login").click(function(e) { 
var interval = setInterval(function(){myFunction();}, 2000); });

Then when im logged out i need to stop the interval:

然后当我注销时,我需要停止间隔:

$("#logout").click(function(e) { 
if(typeof interval !== 'undefined') clearInterval(interval); });

But it doesnt work, i think the way to check if an interval exist is wrong...i can set the interval so it is running when im logged in, but i need to stop/clear it when i click on my Logout button and it doesnt...

但它不起作用,我认为检查间隔是否存在的方法是错误的......我可以设置间隔,以便在我登录时它正在运行,但是当我点击我的注销按钮时我需要停止/清除它并它不...

PS. im using the interval to check "myFunction" automatically every 2 seconds, but maybe there is another way to accomplish this without an interval? thx

附注。我使用间隔每 2 秒自动检查一次“myFunction”,但也许有另一种方法可以在没有间隔的情况下完成此操作?谢谢

回答by jfriend00

Your intervalvariable needs to be declared at a higher scope where it is available to both functions. As you have it now, it is a local variable that ONLY exists within the particular invocation of your event handler function. So, when the next event handler is called, that previous variable no longer exists. You may also want to protect against successive clicks on login:

您的interval变量需要在更高的范围内声明,以便两个函数都可以使用它。正如您现在拥有的那样,它是一个局部变量,仅存在于您的事件处理程序函数的特定调用中。因此,当调用下一个事件处理程序时,先前的变量不再存在。您可能还想防止连续点击登录:

var interval;
$("#login").click(function(e) { 
    if (!interval) {
        interval = setInterval(function(){myFunction();}, 2000); 
    }
});

$("#logout").click(function(e) { 
    clearInterval(interval); 
    interval = null;
});

And, you don't need to check to see if intervalis undefined. You can just call clearInterval()on it and clearInterval()will protect against any invalid argument you pass it.

而且,您无需检查是否intervalundefined。您可以调用clearInterval()它并clearInterval()防止您传递它的任何无效参数。

回答by jameshwart lopez

Here is a simple example where your interval variable should be in global scope for both click events.

这是一个简单的示例,其中您的区间变量应该在两个点击事件的全局范围内。

<!DOCTYPE html>
<html>
<head>
    <title></title>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>  
      <script type="text/javascript">
      $(document).ready(function(){
            function myFunction(){
                var d = new Date();
                var t = d.toLocaleTimeString();
                document.getElementById("demo").innerHTML = t;
            }
            var interval;

            $("#start").click(function(){
                interval = setInterval(function(){
                        myFunction();
                },2000);

            });

            $("#stop").click(function(){
                clearInterval(interval);
            });


      });
      </script>
</head>
<body>
    <p id="demo"></p>
    <button id="start">Start</button>
    <button id="stop">Stop</button>
</body>
</html>