javascript - How to call this function automatically -
im using following function call ajax request, , fill corresponding divs response:
$( function() { $(document).ready(function() { var postdata = ""; $.ajax( { url : \'functions/ajax_api.php?\', type : \'post\', data : postdata, success : function( resp ) { $(\'#id1\').html($(\'#id1\' , resp).html()); $(\'#id2\').html($(\'#id2\' , resp).html()); } }); return false; }); });
the function works fine. question how can call automatically every few seconds?
i tried using window.settimeout(function, 3000) couldnt set correctly.
use setinterval();
instead of .settimeout()
let me little bit that
var interval , setitinterval; // variables can change names interval = function(){ // ajax code here }
to run .. use:
setitinterval = setinterval(interval , 3000);
to stop .. use
clearinterval(setitinterval);
make sure read setinterval more information.
for complete answer , last thing want when using setinterval(); better use visibilitychange
avoid server error , server load or that
document.addeventlistener('visibilitychange',function(){ if(document.visibilitystate == 'visible'){ // user view page }else{ // user not see page } });
Comments
Post a Comment