Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS - How to clear interval after using setInterval() [duplicate]

Is there anyone know how to kill interval after using setInterval() in the following use case?

Thanks in advance!

$(document).ready(function(){
  setInterval(function(){
    $.ajax({ url: "test.php",
      success: function(result){
        $("#results").append(result);
      }
    });
  }, 1000);
});

test.php

$CT = date('Y-m-d H:i:s', time());
echo $CT;
like image 710
Acubi Avatar asked Nov 25 '11 09:11

Acubi


2 Answers

You need to use the clearInterval function

    var interval = setInterval()....
    clearInterval(interval);

http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/

like image 81
leo.vingi Avatar answered Sep 25 '22 05:09

leo.vingi


var interval = setInterval(function() {
    $.ajax({
        url: "test.php",
        success: function(result) {
            $("#results").append(result);
        }
    });
}, 1000);

clearInterval(interval);
like image 21
Yes Barry Avatar answered Sep 26 '22 05:09

Yes Barry