如何停止间隔功能 [英] How to stop interval function

查看:127
本文介绍了如何停止间隔功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm trying to stop with return syntax:

我试图用 return <脚本>
$(document).ready(function(){
setInterval(function(){
var url =../view/anychange.php;
$ .getJSON (url,function(data){
f(data.exists == 0){
alert(0);
} else {
alert(1);
return;
}
});
},5000);


});
< / script>

<script> $(document).ready(function() { setInterval(function() { var url = "../view/anychange.php"; $.getJSON(url, function(data) { f(data.exists == 0){ alert("0"); } else { alert("1"); return; } }); }, 5000); }); </script>

如果我的表中存在数据,函数会每5秒验证一次。

The function verifies every 5 seconds if there exists data in my table.

data.exists == 1 时( alert(1)),我需要停止该函数。 code>)。

I need to stop the function when data.exists == 1 ( the alert("1") ).

推荐答案

<script>
   $(document).ready(function() {
      var id;
      id = setInterval(function() {
         var idRefCopy = id; // You need this otherwise you'll get a reference exception
         var url = "../view/anychange.php";
         $.getJSON(url, function(data) {
         if(data.exists == 0){
           alert("0"); 
         } else {
           alert("1");
           clearInterval(idRefCopy);
           return; 
         }
      });
    }, 5000);
   });
</script>

这篇关于如何停止间隔功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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