|
348 | 348 | | 349.|[Explain types of Memory Leaks in JavaScript?](#q-explain-types-of-memory-leaks-in-javascript)| |
349 | 349 | | 350.|[How accidental closures might cause memory leaks in IE?](#q-how-accidental-closures-might-cause-memory-leaks-in-ie)| |
350 | 350 | | 351.|[How to convert Decimal to Binary in JavaScript?](#q-how-to-convert-decimal-to-binary-in-javascript)| |
| 351 | +| 352.|[How setTimeout() and setInterval() are different from each other ?](#q-how-setTimeout()-and-setInterval()-are-different-from-each-other)| |
| 352 | + |
351 | 353 |
|
352 | 354 | <br/> |
353 | 355 |
|
@@ -8741,3 +8743,44 @@ console.log(val.toString(16)); // A ==> Hexadecimal Conversion |
8741 | 8743 | <div align="right"> |
8742 | 8744 | <b><a href="#">↥ back to top</a></b> |
8743 | 8745 | </div> |
| 8746 | +
|
| 8747 | +## 335Q. ***How setTimeout() and setInterval() are different from each other ?*** |
| 8748 | +
|
| 8749 | +```javascript |
| 8750 | +//Syntax for setTimeout |
| 8751 | + |
| 8752 | +function displayMessage() { |
| 8753 | + console.log('This message will be displayed only once after 4s!') ; |
| 8754 | +} |
| 8755 | + |
| 8756 | +setTimeout(displayMessage, 4000); |
| 8757 | + |
| 8758 | +``` |
| 8759 | +
|
| 8760 | +```javascript |
| 8761 | +//Syntax for setInterval |
| 8762 | + |
| 8763 | +function displayMessage(){ |
| 8764 | + console.log('This message will be displayed after every 4s!') ; |
| 8765 | +} |
| 8766 | + |
| 8767 | +setInterval(displayMessage, 4000) ; |
| 8768 | + |
| 8769 | +``` |
| 8770 | +
|
| 8771 | +Usage : setTimeout( function/expression, timeout, param1, param2, ... ) ; |
| 8772 | +
|
| 8773 | +where expression/function is the JavaScript code to run after the timeout milliseconds have elapsed. The params are optional. |
| 8774 | +
|
| 8775 | +Usage : setInterval ( function/expression, interval, param1, param2, ... ); |
| 8776 | +
|
| 8777 | +where expression/function is the JavaScript code to run repeatedly at specified interval of time has elpased . |
| 8778 | +
|
| 8779 | +Main Difference |
| 8780 | +
|
| 8781 | +When you need to invoke a function/expression once after a specified duration use setTimeout() function. |
| 8782 | +But, if you need to invoke a function/expression repeatedly at a specified interval of time, then you should use setInterval() function. |
| 8783 | +
|
| 8784 | +<div align="right"> |
| 8785 | + <b><a href="#">↥ back to top</a></b> |
| 8786 | +</div> |
0 commit comments