mirror of
https://github.com/microsoft/Web-Dev-For-Beginners.git
synced 2025-08-06 06:47:50 +02:00
js-basics: Fix setTimeout syntax
This commit is contained in:
@@ -139,7 +139,7 @@ function displayDone() {
|
|||||||
console.log('3 seconds has elapsed');
|
console.log('3 seconds has elapsed');
|
||||||
}
|
}
|
||||||
// timer value is in milliseconds
|
// timer value is in milliseconds
|
||||||
setTimeout(3000, displayDone);
|
setTimeout(displayDone, 3000);
|
||||||
```
|
```
|
||||||
|
|
||||||
### Anonymous functions
|
### Anonymous functions
|
||||||
@@ -151,9 +151,9 @@ When we are passing a function as a parameter we can bypass creating one in adva
|
|||||||
Let's rewrite the code above to use an anonymous function:
|
Let's rewrite the code above to use an anonymous function:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
setTimeout(3000, function() {
|
setTimeout(function() {
|
||||||
console.log('3 seconds has elapsed');
|
console.log('3 seconds has elapsed');
|
||||||
});
|
}, 3000);
|
||||||
```
|
```
|
||||||
|
|
||||||
If you run our new code you'll notice we get the same results. We've created a function, but didn't have to give it a name!
|
If you run our new code you'll notice we get the same results. We've created a function, but didn't have to give it a name!
|
||||||
@@ -165,9 +165,9 @@ One shortcut common in a lot of programming languages (including JavaScript) is
|
|||||||
Let's rewrite our code one more time to use a fat arrow function:
|
Let's rewrite our code one more time to use a fat arrow function:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
setTimeout(3000, () => {
|
setTimeout(() => {
|
||||||
console.log('3 seconds has elapsed');
|
console.log('3 seconds has elapsed');
|
||||||
});
|
}, 3000);
|
||||||
```
|
```
|
||||||
|
|
||||||
### When to use each strategy
|
### When to use each strategy
|
||||||
|
Reference in New Issue
Block a user