在Node.js中解释clearTimeout()函数
在node.js中有一个计时器模块,用于安排计时器并在稍后的时间执行特定功能。在Node.js中,使用JavaScript函数clearTimeout()通过使用由特定setTimeout()函数调用返回的timeoutID来中止setTimeout()函数的执行。
如果timeoutID未与任何先前的调用匹配,或者给出了无效的值,则该函数不执行任何操作。setTimeout() JavaScript函数在用户指定的时间或延迟之后执行指定的代码,并返回一个ID,通常称为timeoutID的正整数。
Node.js中clearTimeout()函数的语法
让我们来探讨clearTimeout() JavaScript函数的语法 –
clearTimeout(timeoutID)
在上述语法中,我们在clearTimeout()函数中传递了一个参数,即’timeoutID’。此外,setTimeout() JavaScript函数的函数调用返回的结果是传递给clearTimeout()函数的参数的值,通常表示为timeoutID。
使用这个ID,我们可以终止传递给setTimeout()的函数的执行。clearTimeout()函数返回的结果是undefined,换句话说,我们可以说这个函数返回的是一个未定义的值。
clearTimeout()函数的工作原理
让我们以提醒应用程序为例,演示clearTimeout() JavaScript函数如何操作和使用。
在提醒应用程序中,用户可以指定在多长时间之后发出提醒的时间。在某种情况下,用户可能会提前自行记住某些事情,或者不需要应用程序提醒任务,然后用户可以关闭提醒应用程序以接收警报。
类似于此,setTimeout() JavaScript函数允许程序员在特定的时间后接收警报消息或运行任何特定的代码。
但是,程序员可能不再需要执行该代码,程序员可以通过使用返回值为整数的setTimeout()函数的特定调用的返回值,并将其传递给clearTimeout() JavaScript函数来终止代码的执行。
clearTimeout()函数是一个简单的操作,除非所提供的参数值是尚未执行的setTimeout()函数的成员,否则它不会执行任何操作。
示例
在这个示例中,我们将实现基本的代码来展示setTimeout()函数和clearTimeout()函数的工作,因为要展示clearTimeout()函数的属性,需要先启动定时器,只有通过使用clearTimeout才能停止定时器,请移步到示例。
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;
}
在上面的代码中,首先,我们声明了一些变量,比如time_count
用于计时,isOn
用于检查计时器是否启动,最后,我们还声明了一个变量来存储setTimeout()
函数返回的计时器id。
我们定义了一些方法,比如reset()
用于重置计时器,在该方法中,我们使用clearTimeout()
方法将'tID'
对应的计时器置零。还有两个函数start()
和stop()
,用于启动和停止计时器。
相似的函数
对于setTimeout()
函数,存在一个相似的函数clearTimeout()
,同样地,我们还有一个函数setInterval()
用于周期性调用函数,并且用于清除被setInterval()
方法定义的元素的函数是clearInterval()
,其语法与clearTimeout()
方法相同。让我们看一下在Nodejs中clearInterval()
方法的语法。
clearInterval(Id_of_interval)
使用setTimeout()和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>
在输出中,我们将得到三个按钮和一个显示器。按钮可以启动、停止和重置秒表,而显示器将显示经过的秒数。
在上面的代码中,我们使用上面定义的clearTimeout()函数的语法来定义了秒表的指令。我们使用相同的脚本并得到一个完整工作的秒表。
首先,我们定义了HTML代码的body部分,其中我们定义了用于输入的按钮,而对于脚本部分,我们使用了script标签来添加秒表的工作代码。
在本教程中,我们学习到在Node.js中,JavaScript函数clearTimeout()用于通过使用特定setTimeout()函数调用返回的timeoutID来停止执行setTimeout()函数。
如果timeoutID没有与任何先前的调用匹配,或者提供了无效的值,则此函数不执行任何操作。setTimeout() JavaScript函数会在用户指定的时间或延迟之后执行指定的代码,并返回一个被称为timeoutID的正整数ID。