Async Intro Flashcards

1
Q

What does it mean for a function to be asynchronous?

A

Asynchronousfunctions, as opposed to synchronous functions, are functions that don’t block execution for the rest of the program while they execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does setTimeout() do?

A

setTimeout()is one of the simplest ways to run code asynchronously. It takes two arguments: a callback function and the time to delay execution of the callback, in milliseconds (1/1000th of a second or 1 ms). It sets a timer that waits until the specified delay elapses, then invokes the callback.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what is the syntax for setTimeout()

A

setTimeout(function() { // 1
console.log(‘!’); // 5
}, 3000);

setTimeout(function() { // 2
console.log(‘World’); // 4
}, 1000);

console.log(‘Hello’); // 3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why does code with a 0 timeout still execute after the rest of the code?

A

that code being run bysetTimeoutonly runs when JavaScript isn’t doing anything else. That means that your program must stop running before code executed withsetTimeoutwill even begin to run.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does setInterval differ from setTimeout()?

A

setInterval, does something similar to setTimeout(). Instead of invoking the callback once, though, it invokes it repeatedly at intervals until told to stop. setIntervalreturns an identifier that we can use to cancel the repetitive execution

function save() {
// Send the form values to the server for safekeeping
}

// Call save() every 10 seconds
var id = setInterval(save, 10000);

// Later, perhaps after the user submits the form
clearInterval(id);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly