简体   繁体   中英

jQuery - Require to add setTimeout function will run every 5 seconds to check if jQuery is defined or not upto 1 minute

I would like to add setTimeout function will run every 5 seconds to check if jQuery is defined or up to 1 minute .

$(document).ready(function()
{   
    setInterval(function() {
        setInterval(function() { alert("Message to triggred 5 seconds"); }, 5000);  
            alert("Message to triggred 60 seconds");
        }, 60000);
});

tried to use setTimeout and clearTimeout also, but not enough.

You are creating two interval in your code you just need one and a condition to stop interval.

 var count = 0; var timer = setInterval(function() { if (window.$ || window.jQuery) { clearInterval(timer); alert("jquery found"); return; } count++; if (count >= 12) { clearInterval(timer); alert("Message to triggred 60 seconds"); } alert("Message to triggred 5 seconds"); }, 5000); 

If you just want to know if jQuery loaded or not, you can just check (window.$ || window.jQuery) values at window.onload.

It looks like you're using jQuery to check if jQuery exists. You'd need to use plain javascript to perform the check. Something like this:

var startTime = performance.now();
window.onload = checkJqueryExists;

function checkJqueryExists() {
    var lapsedTime = performance.now() - startTime;
    if (!window.jQuery && lapsedTime < 60000 ) {
        setTimeout(checkJqueryExists, 5000);
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM