Irene T.
Irene T.

Reputation: 1393

Reload a PHP file every x seconds

I need to load a specific PHP file every 20 seconds. I need to loop the load code my code so far is:

<script>
$(document).ready(function($){
$("#myvideos").load("http://www.mydomainz.com/myfile.php");
setTimeout(function () {
$("#myvideos").load("http://www.mydomainz.com/myfile.php");
}, 20000);
});
</script>
<div id="myvideos" style="width:300px; height:250px;"></div>

any ideas please?

Upvotes: 0

Views: 398

Answers (1)

George
George

Reputation: 36786

Wrap your .load() method in a function, taking advantage of it's callback parameter, which will be called once the request is complete. Only then do you want to use setTimeout to call your function and start the process again.

You'll need to call the function initially, when the DOM is ready.

function getMyVideos(){
    $("#myvideos").load("http://www.mydomainz.com/myfile.php", function(){
        setTimeout(getMyVideos, 20000);
    });
}
$(document).ready(getMyVideos);

JSFiddle

Upvotes: 5

Related Questions