
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain clearTimeout Function in Node.js
There is a timer module in node.js that is used to schedule timers and carry out specific functions at a later time. In Node.js the JavaScript function clearTimeout() is used to halt the execution of the setTimeout() function by using the timeoutID returned by a specific setTimeout() function call.
This function does nothing if the timeoutID didn't match for any prior calls or if an invalid value is given. The setTimeout() JavaScript function executes the specified code after the user-specified amount of time or delay, and it returns an ID, which is a positive integer commonly referred to as the timeoutID.
Syntax of the function clearTimeout() in Node.js
Let's explore the clearTimeout javascript function's syntax ?
clearTimeout(timeoutID)
Here in the above syntax we have passed a single parameter here, it is ?timeoutID' in a clearTimeout() function. Additionally, the result returned by the function call of the setTimeout() JavaScript function is the value supplied to the clearTimeout() function's parameter, which is typically represented as timeoutID.
Using this ID, we can terminate the execution of the function passed to setTimeout(). And the function clearTimeout() returns nothing or in other words, we can say that an undefined value is returned by this function.
Working of clearTimeout() function
Let's use a reminder Application as an example to demonstrate how the clearTimeout() JavaScript function operates and is used.
In the reminder application, the user can specify a particular time after how long the application has to sound to give the reminder. There may be a possible case in the event that the user remembers something by themselves ahead of time or does not need the application to remind the task then the user can turn off the reminder application to receive an alert.
Similarly to this, the setTimeout() JavaScript function allows programmers to receive an alert message or to run any particular piece of code after a specific amount of time using the setTimeout() JavaScript function.
But, it might be possible programmer does not need that execution of code anymore and the programmer will want to terminate the execution of the code, this is possible by using the integer value which is the return value of that particular call to setTimeout() function, and passing that to clearTimeout() JavaScript function.
The clearTimeout() function is a straightforward operation that does nothing unless the supplied argument value is a member of a setTimeout() function that has not yet been executed, then the clearTimeout JavaScript function will cancel it, otherwise, it does nothing.
Example
In this example, we will implement the basic code to show the working of the setTimeout() function and clearTimeout() function, because to showcase the property of the clearTimeout() function there is a need to first start the timer which will only stop by using clearTimeout, let's move to the example.
var time_count = 0; var tID; var isOn = false; function reset() { clearTimeout(tID); time_count = 0; console.log(time_count) isTimerOn = false; } function Countedtime() { console.log(time_count) time_count = time_count + 1; tID = setTimeout(Countedtime, 1000); } function start(){ if (isOn == false){ isOn = true; Countedtime(); } } function stop(){ clearTimeout(tID); isOn = false; }
In the above code, first, we have declared some variables, like ?time_count' to count the time, ?isOn' to check whether the timer is on or not, and at last, a variable to store the id of the timer given by the setTimeout() function.
We have defined some methods like reset() to reset the counter and in that, we have used our clearTimeout() method to make the element with ?tID' zero. There are two more functions present that start and stop which are used to start the counter and stop the counter.
Similar Function
As for the setTimeout() function there is the function clearTimeout() function, we have another function that is setInterval() and to remove the element which is defined by the setIntervel() method is clearInterval() which have the same syntax of the clearTimeout() method, let's see syntax of the clearInterval() method in Nodejs ?
clearInterval(Id_of_interval)
Creating Stopwatch using setTimeout() and clearTimeout()
<!DOCTYPE html> <html> <head> <title> Stop-Watch using setTimeout and clearTimeout methods </title> </head> <body> <h1 style="color: black"> Stop-Watch </h1> <div style= "padding-bottom: 10px"> To reset the StopWatch please click the given button: <button onclick="reset_fun()"> Click Me!! </button> </div> <div style= "padding-bottom: 10px"> To start the count please click the givern button : <button onclick="start_fun()">Click Me!!</button> </div> <div style= "padding-bottom: 10px"> Number of seconds passed are: <input type="text" id="show_"> </div> <div style= "padding-bottom: 10px"> To Stop the count press here: <button onclick="stop_fun()">Stop</button> </div> <script> var time_count = 0; var tID; var isOn = false; function reset_fun() { clearTimeout(tID); time_count = 0; document.getElementById("show_").value = time_count; isTimerOn = false; } function Countedtime() { document.getElementById("show_").value = time_count; time_count = time_count + 1; tID = setTimeout(Countedtime, 1000); } function start_fun() { if (isOn == false) { isOn = true; Countedtime(); } } function stop_fun() { clearTimeout(tID); isOn = false; } </script> </body> </html>
In the output we will get three button and one display. Button are to start, stop, and reset the stopwatch, while display will show the number of seconds passed on the stop watch.
In the above code, we have defined the instructions for the stopwatch by using the syntax we have defined above for the clearTimeout() function. We used the same script and get a complete working stopwatch.
First we have defined the body of the html code in which we have defined the buttons to take the input and for the script we have used the script tag to add the code for working of stop watch.
In this tutorial we have learned that in Node.js the JavaScript function clearTimeout() is used to halt the execution of the setTimeout() function by using the timeoutID returned by a specific setTimeout() function call.
This function does nothing if the timeoutID didn't match for any prior calls or if an invalid value is given. The setTimeout() JavaScript function executes the specified code after the user-specified amount of time or delay, and it returns an ID, which is a positive integer commonly referred to as the timeoutID.