1

The following runs every 7 second and calls the showAlert() function. It looks if finds any data through the database. My question is how can I stop it if it finds any data? So there is no need to run again and again.

setInterval(function myalert() {
$.getJSON("handler.php?type=alert&callback=?&cookie="+ y, showAlert);
}, 7000);

on backend this is a part of the php

$alertGroup = mysql_fetch_array($alertQuery);
$json[] = array($title,$text);
echo $callback . "(" . json_encode($json)  . ")";

this is the showAlert

function showAlert(data) {
console.log(data[0][0]);
console.log(data[0][1]);
}
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179

1 Answers1

1

First of all, you have to register your setInterval into a variable to take control of it's instance, look:

var my_interval = setInterval(function myalert() {
$.getJSON("handler.php?type=alert&callback=?&cookie="+ y, showAlert);
}, 7000);

To stop a running timer you should use clearInterval in the instance, look:

clearInterval(my_interval);
RPichioli
  • 3,245
  • 2
  • 25
  • 29