-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetTimeout.js
More file actions
31 lines (22 loc) · 1.24 KB
/
setTimeout.js
File metadata and controls
31 lines (22 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// setTimeout() is a global method that runs a function after a specific amount of time.
// It returns an ID that allows you to clear the timeout if needed using the global clearTimeout() method.
// setTimeout() only runs the function once. Use setInterval() for running a function multiple times on a certain interval.
// Example: Basic usage of setTimeout
function greeting() {
console.log('Hello')
}
// setTimeout takes two arguments: a function and a time in milliseconds (ms)
setTimeout(greeting, 2000) // Calls greeting function after 2000 ms (2 seconds)
// Example: Passing arguments to the callback function
function greeting(name) {
console.log(`Hello ${name}`)
}
setTimeout(() => greeting('Ruslan'), 2000) // Calls greeting('Ruslan') after 2000 ms (2 seconds)
// Example: Clearing timeouts
const id = setTimeout(() => greeting('Ruslan'), 2000) // Calls greeting('Ruslan') after 2000 ms (2 seconds)
// Clear the timeout using the clearTimeout method and providing the ID
clearTimeout(id)
// Common interview question to understand asynchronous behavior
console.log('one') // Logs 'one'
setTimeout(() => console.log('two'), 0) // Schedules the logging of 'two' after the current execution context is clear
console.log('three') // Logs 'three'