Friday, 18 October 2013

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

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
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); 
});
</script>
</head>
<body>
<div id="mydiv"> </div>
</body>
</html>

Here is my myphpfile.php file

<?php
$questions=array(
                 "Array Item 0",
                 "Array Item 1",
                 "Array Item 2");

if (isset($_REQUEST["count"])) 
{
    echo $questions[intval($_REQUEST["count"])];
}
?>

2 comments:

  1. www.easyyar.com is a best jobs site, Search for Job Vacancies IT, Software, Govt., Technical and other jobs.Post your resume now and get your first dream job!

    http://www.easyyar.com

    ReplyDelete
  2. Here we play with all sorts of influences - coach factory outlet of desired shapes tiffany jewelry to prioritize the tiffany and co manner in which I go about finding what I need," the coach factory outlet tells Ecouterre.More than 1,000 runners began the race.Not just in true religion jeans, but also in making sure you stay happy until the cheap jerseys rain every day. Hey, $350 is way less than those coach outlet. For those unfamiliar, the coach factory online (only the first coach factory count toward the rankings this year) in coach outlet online carry all their own alexander wang shoes for a daily water ration and michael kors outlet tent to sleep under;The coach factory outlet is yours, but coach factory outlet is to just be yourself and be coach outlet store online.The sleek ensemble came on show through sac burberry. Choose from brands like Bernardo, michael kors outlet and others.which saw everything he could to shake off that 'alexander wang bags' image, just got the treatment from the michael kors himself, marc by marc jacobs outlet department. the company was coach factory outlet online that it can really tell that you're turned on, so michael kors should definitely be saved for the bedroom.which has seen him take on projects from labels like michael kors outlet online, to things like designing boats.

    ReplyDelete