Asynchronous JavaScript
Synchronous vs Asynchronous
Synchronous code executes line by line. Each line must finish before the next one starts.
Asynchronous code allows the program to start a long-running task and continue running other code while waiting for the task to finish.
Callbacks
A callback is a function passed as an argument to another function. This technique allows a function to call another function. A callback function can run after another function has finished.
Timing Events
JavaScript allows execution of code at specified time intervals.
setTimeout(function, milliseconds): Executes a function after waiting a specified number of milliseconds.setInterval(function, milliseconds): Same as setTimeout(), but repeats the execution of the function continuously.
Summary
- Asynchronous code runs in parallel with other code (non-blocking).
- Callbacks are functions passed as arguments to be executed later.
setTimeoutruns code once after a delay.setIntervalruns code repeatedly at intervals.
Quick Quiz
Which function is used to stop an interval timer?
Enjoying these tutorials?