SetTimeout recently encountered a Javascript question in the Javascript class. The content is as follows:
Try to implement the Javascript code in the comments section and add more
Code (if it cannot be implemented, describe the reason why it cannot be implemented ):
Var Obj = function (msg ){
This. msg = msg;
This. shout = function (){
Alert (this. msg );
}
This. waitAndShout = function (){
// Execute the above shout method every five seconds
}
}
Var testObj = new Obj ("Hello, World! ");
TestObj. shout (); frankly speaking, I didn't have the experience of using setTimeout/setInterval in Javascript classes before, so I thought it was unfeasible at first. However, after careful consideration, we can find that this can be achieved. To put it another step, it is very easy to execute a specific statement every five seconds. For example, if you do not consider other factors, the function in the question can be written as follows:
This. waitAndShout = function (){
SetTimeout ('this. shout () ', 5000 );
} After running, everyone will realize that this variable cannot be found. But why? It will soon be realized that setTimeout/setInterval is a method of the window object, so it can also be written as window. setTimeout/window. setInterval, then the preceding this. shout () is very easy to understand why it cannot be executed, because it actually calls window. shout ().
After knowing the cause, it will be very easy to solve. You only need to bind the object to the window object (I am excited about the interesting Javascript Object mechanism ). Then, make a small modification to the above function:
Window. setTimeout ('window. Obj. shout () ', 5000); in addition, I also thought of saving the object as an array and then referencing the call. The Code is as follows:
ObjectClass. cnt = 0;
ObjectClass. objects = new Array ();
Function ObjectClass_method (){
SetTimeout ('objectclass. objects ['+ this. id +']. method (); ', 5000 );
}
Var obj1 = new ObjectClass ('feelingbucket ');
Obj1.method (); however, I personally feel that the first method is much clearer.
Note: Javascript seems to be a lot of places to be cautious with, especially the object mechanism. As I said before, Javascript is not more complex than other languages, but it is not as simple as you think.
PS: After this problem is completed, Google finds that other brothers have already solved this problem. For example, here is the problem. For more information, see.
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