admin管理员组

文章数量:1415653

I'm trying to stop with return syntax:

<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>

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

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

I'm trying to stop with return syntax:

<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>

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

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

Share Improve this question edited Jan 23, 2014 at 15:04 lante 7,3564 gold badges38 silver badges59 bronze badges asked Jan 23, 2014 at 14:56 Jhonatan SandovalJhonatan Sandoval 1,2936 gold badges24 silver badges40 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5
<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>

You have to define the interval inside a variable, and then clear it. Try this:

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


   });
</script>

You have a typo in the code (i have fixed it here, its a "f" instead of "if" ;) Hope this helps.

You need to clear your interval, this will prevent your function from being fired again. See this for interval reference.

This should be your code:

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

How about clearInterval?

var myVar = setInterval(function(){myTimer()},1000);

function myTimer()
{
    var d = new Date();
    var t = d.toLocaleTimeString();
    document.getElementById("demo").innerHTML=t;
}

function myStopFunction()
{
    clearInterval(myVar);
}

source: W3Schools

I strongly suggest you do not hit your server unless you know your ajax was done

This example will check, but only after the server returned

var counter = 0;
function checkData() {
  var url = "../view/anychange.php";
  $.getJSON(url, function(data) {
    if (data.exists == 0) {
      $("#someContainer").html("Not yet on attempt #"+(counter++));
      setTimeout(checkData,5000);
    } else {
      $("#someContainer").html("found on attempt #"+(counter++));
    }
  });
}
$(function() {
  checkData();
});

本文标签: javascriptHow to stop interval functionStack Overflow