Part 31 | JavaScript Tutorial | Callbacks | setInterval and setTimeout in JavaScript | Event Looping

Published: 04 March 2023
on channel: Trinits Technologies
59
0

1. Callback functions:-
---------------------
Callback function is a function that is passed as an argument to another function and is executed when that function finishes its execution.

The purpose of a callback function is to provide a way for a function to asynchronously return a result or execute some code after it has completed.
In the below example, callBackFunc is function.

function sumValue(x, y, callBackFunc){
//Call the server to get the data from server.
let sum = x + y;

//Once data is received,invoke call back function.
callBackFunc(sum);
}

function printFunc(value) {
console.log(value);
}


1. setTimeout(function, milliseconds)
If we want to execute any code only one time but after specifying the particular time, then we can use the setTimeout method.

2. setInterval(function, milliseconds)
If we want to execute any code multiple times based on time interval then we can use the setTimeout method.

3. clearTimeout()
4. clearInterval()

Event looping in JS:-
-------------------
JavaScript is a single-threaded language, which means that it can only execute one piece of code at a time. However, it also has a built-in mechanism for handling asynchronous operations, known as the event loop.

The event loop is a continuously running loop that checks for new events in the event queue and processes them one by one. When an asynchronous operation is initiated, such as a setTimeout or a network request, it is added to the event queue and the event loop continues to run. When the operation is completed, a callback function is added to the event queue to be executed in the future.

console.log("start");
console.log("end");

Note that even though the setTimeout function was called with a delay of 0 milliseconds, it still waited until the main thread was finished before executing the callback. This is because it is an asynchronous operation that is queued up by the event loop.


On this page of the site you can watch the video online Part 31 | JavaScript Tutorial | Callbacks | setInterval and setTimeout in JavaScript | Event Looping with a duration of hours minute second in good quality, which was uploaded by the user Trinits Technologies 04 March 2023, share the link with friends and acquaintances, this video has already been watched 59 times on youtube and it was liked by 0 viewers. Enjoy your viewing!