jQuery settimeout and setinterval; In this tutorial, you will learn how to use set an interval time using jQuery setTimeout(seconds) & setInterval(seconds) methods.
When you work with jQuery, You must know some of the basic jQuery function. It functions very helpful for web development.
jQuery setInterval & setTimeout function
Here are setTimeout and setInterval function with examples:
- setTimeout
- setInterval
About jQuery setTimeout Method
The setTimeout function executes a script code after a specified delay (in milliseconds).
Syntax of jQuery setTimeout Method
setTimeout(function, milliseconds);
Example of setTimeout method
This example will demostrate you how use this setTimeout function.
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script> $(document).ready(function(){ $("#btn-timout").click(function(){ setTimeout("alert('Hello World!');", 4000); }); }); </script> </head> <body> <div class="common_box_body"> <button id="btn-timout">Click</button> <p>Click the above given button and wait for 4 seconds. After 4 second will come alert box</p> </div> </body> </html>
Output
Click the above given button and wait for 4 seconds. After 4 second will come alert box
About jQuery setInterval Method
The jQuery setInterval function can be used to continue any work by using a regular time-based trigger.
Syntax of setInterval Method
setInterval(function, milliseconds);
Example of setInterval method
This example will demostrate you how use this setInterval function.
<!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> <script> $(document).ready(function(){ $("#btn-interval").click(function(){ setInterval("alert('Hello World!');", 4000); }); }); </script> </head> <body> <div class="common_box_body"> <button id="btn-interval">Click</button> <p>Click the above given button and wait for 4 seconds. After 4 second will come alert box continuously</p> </div> </body> </html>
Output
Click the above given button and wait for 4 seconds. After 4 second will come alert box continuously
To Remember
The setInterval function performs again and again in the given interval time.The setTimeout function is executed only once in given intervals time. In both functions, 1000 ms = 1 seconds.
Recommended jQuery Tutorial
- jQuery | Event MouseUp () By Example
- Event jQuery Mouseleave By Example
- jQuery Event Mouseenter Example
- Event jQuery MouseOver() & MouseOut By Example
- keyup jquery event example
- Jquery Click() Event Method with E.g.
- Event jQuery. Blur By Example
- jQuery form submit event with example
- keydown function jQuery
- List of jQuery Events Handling Methods with examples
- Jquery Selector by .class | name | #id | Elements
- How to Get the Current Page URL in jQuery
- jQuery Ajax Get() Method Example
- get radio button checked value jquery by id, name, class
- jQuery Set & Get innerWidth & innerHeight Of Html Elements
- jQuery Get Data Text, Id, Attribute Value By Example
- Set data attribute value jquery
- select multiple class in jquery
- How to Remove Attribute Of Html Elements In jQuery
- How to Checked Unchecked Checkbox Using jQuery
- jQuery removeClass & addClass On Button Click By E.g.
- To Remove whitespace From String using jQuery
- jQuery Ajax Post() Method Example
- jQuery Ajax Get() Method Example
- To Load/Render html Page in div Using jQuery Ajax $.load