Skip to content

Commit 840a25d

Browse files
committed
Update README.md
1 parent 1e04c81 commit 840a25d

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

README.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,47 @@ setInterval(function() { console.log("Good morning"); }, 2000);
20082008
<b><a href="#">↥ back to top</a></b>
20092009
</div>
20102010
2011+
## Q. ***How setTimeout() and setInterval() are different from each other?***
2012+
2013+
```javascript
2014+
//Syntax for setTimeout
2015+
2016+
function displayMessage() {
2017+
console.log('This message will be displayed only once after 4s!') ;
2018+
}
2019+
2020+
setTimeout(displayMessage, 4000);
2021+
2022+
```
2023+
2024+
```javascript
2025+
//Syntax for setInterval
2026+
2027+
function displayMessage(){
2028+
console.log('This message will be displayed after every 4s!') ;
2029+
}
2030+
2031+
setInterval(displayMessage, 4000) ;
2032+
2033+
```
2034+
2035+
Usage : setTimeout( function/expression, timeout, param1, param2, ... ) ;
2036+
2037+
where expression/function is the JavaScript code to run after the timeout milliseconds have elapsed. The params are optional.
2038+
2039+
Usage : setInterval ( function/expression, interval, param1, param2, ... );
2040+
2041+
where expression/function is the JavaScript code to run repeatedly at specified interval of time has elpased .
2042+
2043+
Main Difference
2044+
2045+
When you need to invoke a function/expression once after a specified duration use setTimeout() function.
2046+
But, if you need to invoke a function/expression repeatedly at a specified interval of time, then you should use setInterval() function.
2047+
2048+
<div align="right">
2049+
<b><a href="#">↥ back to top</a></b>
2050+
</div>
2051+
20112052
## Q. ***Why is JavaScript treated as Single threaded?***
20122053
20132054
JavaScript is a single-threaded language. Because the language specification does not allow the programmer to write code so that the interpreter can run parts of it in parallel in multiple threads or processes. Whereas languages like java, go, C++ can make multi-threaded and multi-process programs.
@@ -8743,44 +8784,3 @@ console.log(val.toString(16)); // A ==> Hexadecimal Conversion
87438784
<div align="right">
87448785
<b><a href="#">↥ back to top</a></b>
87458786
</div>
8746-
8747-
## Q. ***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

Comments
 (0)