javascript - Execute a function every 3 seconds in jquery mobile -
am trying create function in jquery mobile autorefreshes every 3 seconds when on page.
i have tried:
$(document).on('pageshow', '#chat',function(){ function autoload(){ console.log('its after 3 sec') } autoload(); });
how can change function console.log('its after 3 sec') after 3 seconds how can add time interval.the function should execute when 1 on page(#chat)
you can use setinterval method, execute specified function @ desired interval (in milliseconds).
$(document).on('pageshow', '#chat', function() { function autoload() { console.log('its after 3 sec') } setinterval(autoload(), 3000); });
to stop execution when hiding page, store interval id , use clearinterval method.
// store interval id can clear when page hidden var intervalid; $(document).on('pageshow', '#chat', function() { function autoload() { console.log('its after 3 sec') } intervalid = setinterval(autoload(), 3000); }); $(document).on('pagehide', function() { clearinterval(intervalid); });
you can use settimeout method, similar setinterval method.
// store timeout id can clear when page hidden var timeoutid; $(document).on('pageshow', '#chat', function() { function autoload() { console.log('its after 3 sec') timeoutid = settimeout(autoload(), 3000); } autoload(); }); $(document).on('pagehide', function() { cleartimeout(timeoutid); });
Comments
Post a Comment