Async Intro Flashcards
What does it mean for a function to be asynchronous?
Asynchronousfunctions, as opposed to synchronous functions, are functions that don’t block execution for the rest of the program while they execute.
What does setTimeout() do?
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.
what is the syntax for setTimeout()
setTimeout(function() { // 1
console.log(‘!’); // 5
}, 3000);
setTimeout(function() { // 2
console.log(‘World’); // 4
}, 1000);
console.log(‘Hello’); // 3
Why does code with a 0 timeout still execute after the rest of the code?
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 does setInterval differ from setTimeout()?
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);