简体   繁体   English

clearTimeout没有清除超时

[英]clearTimeout isn't clearing the timeout

Trying to clear a timeout and its not working. 试图清除超时并且无法正常工作。 console.logging it returns a number after its been initialized and after it's been destroyed. console.logging它在初始化之后和它被销毁之后返回一个数字。

  const timer = setTimeout(() => {}); console.log('initialised', timer); // initialised 22 clearTimeout(timer); console.log('destroyed', timer); // destroyed 22 

I'm expecting the second log to return null. 我期待第二个日志返回null。 I also didn't expect the timer to be a simple number. 我也没想到计时器是一个简单的数字。 I would have expected it to be an object. 我本来以为它是一个对象。 Is clearTimeout doing what it should be doing here? clearTimeout正在做它应该在这里做什么吗?

The timer variable holds a unique number that represents a particular timer. timer变量包含表示特定计时器的唯一编号。 Clearing the timer stops the asynchronous operation, but your variable is still just that, a variable, and it will hold the last value you gave it unless it goes out of scope or your assign something else to it. 清除计时器会停止异步操作,但是你的变量仍然只是一个变量,并且它将保留你给它的最后一个值,除非它超出范围或你为它分配其他东西。

The best proof that your code is working as it should is to give the timer callback function some behavior and see if that behavior is correct. 您的代码正常工作的最好证明是给定时器回调函数一些行为,并查看该行为是否正确。 With your code, we'll see that the timer callback function never actually runs (no message from the callback function is ever written to the console) because you are cancelling it before the function that created it completes. 使用您的代码,我们将看到计时器回调函数从未实际运行(回调函数中没有消息写入控制台),因为您在创建它的函数完成之前取消它。 We don't need extra console.log() statements beyond that one. 除此之外,我们不需要额外的console.log()语句。

 const timer = setTimeout(() => { console.log("The timer function has run!" ); }); clearTimeout(timer); 

NOTE: In most cases, we don't need to know the actual value of the timer variable. 注意:在大多数情况下,我们不需要知道timer变量的实际值。 We only need to store it to kill its corresponding timer operation later. 我们只需要存储它就可以在以后杀死相应的定时器操作。

clearTimeout is doing exactly what is expected of it. clearTimeout完全符合预期。 When you create a timeout, it returns a unique number, in your case you saved it in "timer" variable. 当您创建超时时,它会返回一个唯一的数字,在您的情况下,您将其保存在“timer”变量中。 Now when you clear timeout it just stops the setTimeout execution, but does not remove variable "timer" 现在当你清除超时它只是停止setTimeout执行,但不删除变量“timer”

I'm expecting the second log to return null. 我期待第二个日志返回null。

The timer is a simple variable holding a number (ID of the timer). timer是一个包含数字(计时器ID)的简单变量。 Doing clearTimeout does not change its value, but clears a timeout , ie stops the timer. 执行clearTimeout不会更改其值,但会清除超时 ,即停止计时器。

I would have expected it to be an object 我本来以为它是一个对象

Why though? 为什么呢? There is nothing to store in it. 没有什么可以存储在其中。 It is a simple number, starting from 1 for a very first timer, 2 for a second one, etc. 这是一个简单的数字,从第一个计时器的1开始,第二个计时器从2开始,等等。

setTimeout defines a timer and returns a number that is the unique id of that timer. setTimeout定义一个计时器并返回一个数字,该数字是该计时器的唯一ID。 clearTimeout gets a timer id and cancels it. clearTimeout获取一个计时器ID并取消它。

Since number values in JavaScript are immutable and you are basically sending the timer variable's value to the clearTimeout function, the language, or the browser, cannot change the value of the variable, hence the number will not change in the scope of your function, and the result is that you still see the same number in both logs. 由于JavaScript中的数值是不可变的,并且您基本上将timer变量的值发送到clearTimeout函数,因此语言或浏览器不能更改变量的值,因此数字不会在函数范围内更改,并且结果是您仍然在两个日志中看到相同的数字。

It does, however, cancels the timeout, so that the call to the function you sent to setTimeout will not be called, as long as you called clearTimeout before it was called. 但是,它会取消超时,因此只要在调用clearTimeout之前调用clearTimeout ,就不会调用对发送到setTimeout的函数的调用。

Here is more information about setTimeout and clearTimeout . 以下是有关setTimeoutclearTimeout的更多信息。

Let's understand the return value of these functions. 让我们理解这些函数的返回值。

The returned timeoutID is a positive integer value which identifies the timer created by the call to setTimeout(); 返回的timeoutID是一个正整数值,它标识通过调用setTimeout();创建的计时器setTimeout();

This value can be passed to clearTimeout() to cancel the timeout. 可以将此值传递给clearTimeout()以取消超时。

It is guaranteed that the setTimeout() will always return an ID (number) not an object. 保证setTimeout()始终返回ID(数字)而不是对象。

The return value of clearTimeout() is undefined . clearTimeout()的返回值undefined

Also, I would like to add one more point, in Javascript the type number is always passed by value. 另外,我想再补充一点,在Javascript中,类型number始终按值传递。 So, you shouldn't be expecting it to change while you just pass it to a function. 因此,当您将其传递给函数时,不应期望它会发生变化。 Like in your case, 就像你的情况一样

clearTimeout(timer);

An invocation like this can never change the value(at source) of the variable(type number ) passed. 像这样的调用永远不会改变传递的变量(类型number )的值(在源头)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM