This is the 19th day of my participation in the More text Challenge. For details, see more text Challenge
On paper come zhongjue shallow, must know this to practice. Even if it is an insignificant little knowledge at ordinary times, we also need to study with a serious attitude, otherwise, when might step on the pit, hurt each other!
foreplay
No matter whether the article is water or not, foreplay must be done enough, otherwise it can’t go on writing, O(∩_∩)O haha ~
After Posting the article “A simple code implementation of front-end JavaScript” stabilization “, a friend posted this comment:
I have a habit of writing code where I assign null to variables that have already been destroyed, like this:
It is said that the destruction is more thorough.
I am not sure if the above suggestion is correct. It seems that it is rare to see the operation of assigning null after cleatTimeout.
In the case of uncertainty, I believe in only one principle — practice is the only criterion for testing truth. If there is any confusion, let’s start to verify it. Yes, I am so direct, please don’t be surprised! Jennifer
accident
Originally thought it was a very simple verification, just sprinkle water! But, who wants to have an accident, do not believe you see:
What? ! The return value of setTimeout is a number!! Just ask you: surprised not surprise not surprise?
I’ve been doing development for a few years and I’m so weak that I didn’t know that. Then again, who is going to print its return value? We use its function.
Why is this happening? Let’s see what MDN says:
The return value timeoutID is a positive integer indicating the number of the timer. This value can be passed to clearTimeout() to cancel the timer.
It seems that this is a common sense problem, only blame me at ordinary times did not pay attention to ah, look at ordinary times to strengthen the reserve of basic knowledge!
As to why the value of the timer keeps increasing, the MDN explains:
On the same object (a window or worker), setTimeout() or setInterval() will not reuse the same timer number for subsequent calls. But different objects use separate pools of numbers.
The nature of each execution of the Timer is that a new timer is generated, belonging to a different object, so the number changes.
I was going to look at setInterval again, but this explanation put me off the idea of validation, which would be another “surprise”.
validation
After the previous accident, LET me know my ignorance. But the accident is also the best spur, even ashamed, but the beginning of the verification or have to go down.
Now that we know the truth: the return value of setTimeout is a number that represents the unique identity of the delayer object, what exactly does its value become after clearTimeout()? Look at the big screen:
We see that after the clearTimeout() method is called to destroy the timer, the value of the timer is not cleared.
conclusion
After the above verification, we can draw the following conclusions:
- The return value of the setTimeout() method is a number that represents the unique id of the timer;
- This number is the lifetime achievement of the timer. The number will not change during the execution of the timer.
- The timer setInterval and the timer setTimeout share a pool of numbers, and none of the numbers are the same.
- The timer number is not cleared after the timer destruction methods (clearTimeout and clearInterval) are called.
The above summary applies to all timers (timers and delayers).
Well, it seems that I readily give a null practice or more reasonable, after all, is played so a silk of the role of ( ̄)  ̄).
Null is a good habit! ( ̄▽ ̄)~*
Null is a good habit! ( ̄▽ ̄)~*
Null is a good habit! ( ̄▽ ̄)~*
In fact, today’s verification also confirmed another truth: we usually ignore the most, often we think the most familiar, hurt each other and do not know!
You taste, you fine taste!
~
~
Thanks for reading!
Learn interesting knowledge, make interesting friends, and create interesting souls!
Everybody is good! I am the author of “programming Samadhi” hermit King, my public number is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!
Both knowledge and skill, internal force and external work, both theory and practice should grasp, both hands should be hard!