What's the best way to keep executing a JS code (each 100 ms in m case) until the document is ready.
setInterval(function() {
xajax_updateLoader();
}, 100);
One the document is ready. The execution should stop.
var updateInterval;
$(function(){
updateInterval= setInterval(function() {
xajax_updateLoader();
}, 100);
});
$(windows).load(function(){
clearInterval(updateInterval)
});
var interval = setInterval(function() { ... }, 100);
window.onload = function() { clearInterval(interval); }
This clears the interval on the onload event.
var handle = setInterval(function() {
xajax_updateLoader();
if (jQuery.isReady) {
//DOM is ready
clearInterval(handle);
}
}, 100);
Like this
domReady = false;
var ctx = setInterval(function() {
if (domReady === true)
{
clearInterval(ctx);
}
// your code here
}, 100);
if (typeof document.addEventListener !== 'undefined') // chrome / safari / firefox
{
document.addEventListener("DOMContentLoaded", function(){
domReady = true;
document.removeEventListener('DOMContentLoaded');
}, false);
}
else if (typeof document.attachEvent !== 'undefined') // IE
{
document.attachEvent("onreadystatechange", function(){
if ( document.readyState === "complete" )
{
document.detachEvent( "onreadystatechange", arguments.callee );
domReady = true;
}
});
}
Use clearInterval()
var interval = setInterval(function() { ... }, 100);
$(document).ready(function(){
clearInterval(interval);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.