0

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.

sandip
  • 533
  • 9
  • 29

2 Answers2

1

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.

Clujio
  • 332
  • 1
  • 6
1

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);
    }
}
CDelaney
  • 1,238
  • 4
  • 19
  • 36