Javascript directs this in the function parameter in setinteval to a specific object.
Source: Internet
Author: User
I was blinded when I saw this question, because I didn't clear it at that time and thought about it for a long time. Then I came to the Internet to check it, A foreign website found that the scope of the functions after setinterval and setTimeout is global, that is, this points to a global object.
This problem is troublesome. I often need to use this in the loop function to reference the current object. Maybe you can use a closure, but the actual situation is not so simple. When there are more object instances, the closure is also messy.
My wish is to make this in the loop function still point to the current context object, without passing parameters or closures (in fact, this is also a closure, but it looks more natural in form );
Example: (Part 1 CodeTo send requests regularly)
Copy codeThe Code is as follows:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service