JAVASCRIPT Tutorial
setTimeout()
to schedule code to run once after a delay (in milliseconds):setTimeout(() => {
console.log("Code executes after 5 seconds");
}, 5000);
setInterval()
to schedule code to run repeatedly at an interval:setInterval(() => {
console.log("Code executes every 2 seconds");
}, 2000);
clearTimeout()
or clearInterval()
to stop a previously set timer:// Clear a timeout
clearTimeout(timeoutId);
// Clear an interval
clearInterval(intervalId);
async
functions and promises to control the order of execution.console.log("Start");
setTimeout(() => {
console.log("Code runs after 2 seconds");
}, 2000);
console.log("Continue");
Output:
Start
Continue
Code runs after 2 seconds
This demonstrates how JavaScript code continues to execute even after a setTimeout()
call. The "Code runs after 2 seconds" message appears after the "Continue" message, indicating asynchronous execution.