How to delay document.ready() method until a variable is set in jQuery ? Last Updated : 23 Feb, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will learn to delay document.ready until a variable is set. The document.ready() method is used to execute some jQuery or JavaScript scripts when the HTML DOM is completely loaded. There are two approaches that can be followed here: Approach 1: Using the holdReady() method in the jQuery library and the setTimeout() method. First we set the parameter in the holdReady() method to true to hold the execution of the document.ready() method. Then, a timeout function with an appropriate delay time can be added using the setTimeout() method. Within this timeout method, a variable is defined and subsequently the holdReady() is called once again but this time the parameter is set to false to release the execution of the document.ready() method. Lastly, the document.ready() method is called and within this method, the variable is now set and a division element is displayed stating that the variable has been set. The content of the division element is set using the text() method in jQuery. Example: In this example, there is a delay of 3 seconds (3000 milliseconds) in the setTimeout() method after which the variable is set and the HTML DOM is fully loaded. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <!-- Basic inline styling --> <style> body { text-align: center; } h1 { color: green; font-size: 40px; } div { font-size: 30px; font-weight: bold; } </style> </head> <body> <h1></h1> <!-- The division element --> <div></div> <script type="text/javascript"> $.holdReady(true); setTimeout(function () { // Setting a variable until // document.ready is called const myVar = "GFG"; $.holdReady(false); }, 3000); $(document).ready(function () { $("h1").text("GeeksForGeeks"); $("div").text("The variable has been set!"); }); </script> </body> </html> Output: Approach 2: Using the holdReady() method in the jQuery library and the setInterval() method. This approach is very identical to the previous approach but the key distinctions lie in the syntax of the document.ready method which is represented as $(function(){ .. }) & the setInterval() method is used instead of the setTimeout() method. Usually, we use the setInterval() method to repeat the passed function at some time interval but in this case, it serves the same purpose as the setTimeout() method. All the underlying logic remains the same as before. Example: In this example, there is a delay of 2 seconds (2000 milliseconds) in the setInterval() method after which the variable is set and the HTML DOM is fully loaded. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"> </script> <!-- Basic inline styling --> <style> body { text-align: center; } h1 { color: green; font-size: 40px; } p, div { font-size: 30px; font-weight: bold; } </style> </head> <body> <h1></h1> <p></p> <!-- The division element --> <div></div> <script type="text/javascript"> $.holdReady(true); setInterval(() => { // Setting a variable until // document.ready is called var myBoolean = true; $.holdReady(false); }, 2000); // Same as $(document).ready(function(){..}) $(function () { $("h1").text("GeeksForGeeks"); $("p").text( "jQuery - Delay document.ready until a variable has been set" ); $("div").text( "The boolean variable has been set!"); }); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to delay document.ready() method until a variable is set in jQuery ? R rajatsandhu2001 Follow Improve Article Tags : Web Technologies JQuery Geeks Premier League Geeks-Premier-League-2022 HTML-Tags CSS-Properties jQuery-Methods jQuery-Questions +3 More Similar Reads How to run a code on document ready event in jQuery ? In this article, we will see how to run a code when a page loads using jQuery. To run a code at page load time, we will use the ready() method. This method helps to load the whole page and then execute the rest code. This method specifies the function to execute when the DOM is fully loaded. Syntax: 1 min read What is the use of delay() method in jQuery ? In this article, we will see how to use the delay() method and why to use it in jQuery. The delay() method is used to set a timer to delay the execution of the next item in the queue. Syntax: $(selector).delay(para1, para2); In the below example, first, we create a div of size 250px X 200px and set 1 min read How to use JavaScript variables in jQuery selectors ? In this article, we will discuss how to use JavaScript variables in jQuery selectors. In the following examples, it can be seen that we have used the values stored in JavaScript Variables used inside the jQuery Selectors. Example 1: The concatenation technique can be applied in order to use the valu 2 min read How to append an element in two seconds using jQuery ? Given an HTML element and the task is append an element in the document after few seconds using fadeIn effect with the help of JQuery. Approach: Select the target element to append the element. Use one of the appendTo() or append() method to append the element. Example 1: In this example, the <di 2 min read How to use hide() method in jQuery ? The hide() method in jQuery is used for hiding the selected web elements. In this article, we will discuss the hide() method in detail. This method is generally used for effects or animation in jQuery. It also allows us to animate the behavior (transition) of hiding any specific element. Syntax:.hid 5 min read Which function is used to prevent code running before document loading in jQuery ? In this article, we will learn how to prevent code from running, before the document is finished loading. Sometimes we might need to run a script after the entire document gets loaded, for example, if we want to calculate the size of images in the document then we need to wait until the entire docum 2 min read What is the use of ready() function in jQuery ? In this article, we will see how to use ready() function provided by the jQuery library. The ready() function is used to execute some javascript code only when the HTML DOM is fully loaded. We should not manipulate the DOM until it is fully loaded. ready() method comes very handy to detect when the 1 min read How to use keyup with delay in jQuery ? In this article, we will see how to use keyup with a delay in jQuery. There are two ways to achieve the same: Approach 1: Using the keypress(), fadeIn(), delay() and fadeOut() methods in the jQuery library and clearTimeout() and setTimeout() methods in native JavaScript. Firstly, a variable keyupTim 4 min read How to wrap setTimeout() method in a promise ? To wrap setTimeout in a promise returned by a future. We can wrap setTimeout in a promise by using the then() method to return a Promise. The then() method takes up to two arguments that are callback functions for the success and failure conditions of the Promise. This function returns a promise. Th 2 min read How to change the value of a global variable inside of a function using JavaScript ? Pre-requisite: Global and Local variables in JavaScript Local Scope: Variables that are declared inside a function is called local variables and these are accessed only inside the function. Local variables are deleted when the function is completed. Global Scope: Global variables can be accessed fro 2 min read Like