How to use setInterval and clearInterval jQuery methods in PHP?
How to use setInterval and clearInterval jQuery methods in PHP?
I will explain usage of setInterval and clearInterval jQuery methods with the help of simple example in PHP. Suppose there is a PHP array in which there are 3 elements. I want to display all of them one by one at regular intervals and stop when all the three array elements have been displayed. I will use setInterval and clearInterval jQuery methods here to implement it.
Here is my index.php file
var count = 0;
$(document).ready(function(){
var auto_refresh = setInterval(function (){
$('#mydiv').load('myphpfile.php', {count: count}, function () {
count = count + 1;
//after three attempts it won't call php file.
if (count > 2) {
clearInterval(auto_refresh);
}
}).fadeIn("slow");
}, 1000);
});
Here is my myphpfile.php file
$questions=array(
"Array Item 0",
"Array Item 1",
"Array Item 2");
if (isset($_REQUEST["count"]))
{
echo $questions[intval($_REQUEST["count"])];
}
?>