Javascript For Deferring Until jQuery Loaded

Allure Web Solutions Code Snippet

This snippet is for deferring running of any jQuery code until jQuery is loaded onto the page. This function is useful if you need to call your jQuery somewhere on your page, rather than in a specific JS file that you know is being loaded after jQuery. One downside of the method is that if jQuery for some reason is never loaded, the timeout function will continue to run indefinitely, which could have a negative impact on performance.

The Defer Function

(function defer(method) {
  if (window.jQuery) {
  } else {
    setTimeout(function () {
    }, 50);

// Credits:

The defer function will call itself every 50ms until window.jQuery exists at which time it exits and call method()


defer(function () {
    // do something

// OR

function doSomething() {
    // do something
Mike Doubintchik

Author Mike Doubintchik

More posts by Mike Doubintchik

Leave a Reply