Using SetTimeout & window.settimeout with parameters in a Javascript class (parameters can be objects)

Source: Internet
Author: User
Tags closure setinterval
Recently encountered a Javascript exam, which reads as follows:

Try to implement the Javascript code in the annotation section to add more anywhere else
Code (if not implemented, explain why it cannot be implemented):

var Obj = function (msg) ... {
this.msg = msg;
This.shout = function () ... {
alert (this.msg);
}

This.waitandshout = function () ... {
Perform the above shout method after five seconds
}
}

var testobj = new OBJ ("hello,world!");
Testobj.shout ();
Frankly, I didn't use settimeout/setinterval experience in Javascript, so I started off thinking it was impossible. But after careful deliberation, it can be realized. To step back, executing a paragraph every five seconds is very easy to implement. For example, without considering other factors, the function in the topic can be written as follows:

this. waitandshout = function () ... {
SetTimeout (' This.shout () ', 5000);
}
After running, anyone will realize that this variable is not found. But this is why, it is very soon to realize that Settimeout/setinterval is actually a method of window object, so can also be written as Window.settimeout/window.setinterval, then the above this Shout () is very easy to understand why it cannot be performed because it actually calls Window.shout ().

Knowing the cause is a lot easier to solve, as long as you bind the object to the Window object (I'm excited about the interesting object mechanism of Javascript). So, the above function makes a minor modification:

this. waitandshout = function () ... {
Window. OBJ = this;
SetTimeout (' Obj.shout () ', 5000);
}
That's it. As a matter of fact

SetTimeout (' Obj.shout () ', 5000);
Equivalent to

Window.settimeout (' window. Obj.shout () ', 5000);
Also, before I thought about saving an object as an array, and then referencing the call, the code was as follows:

function objectclass (property) ... {
This.property = property;
This.id = objectclass.cnt;
objectclass.objects[objectclass.cnt++] = this;
This.method = Objectclass_method;
}

objectclass.cnt = 0;
objectclass.objects = new Array ();

function Objectclass_method () ... {
settimeout (' objectclass.objects[' + this.id + '].method (); ', 5000);
}

var obj1 = new objectclass (' Feelinglucky ');
Obj1.method ();
But personal feeling is still much clearer than the first of these methods.

PostScript, Javascript does seem to be a lot of places to be cautious, especially the object mechanism. As I said before, Javascript is no more complicated than other languages, but it's not as simple as you might think.

PS: After the completion of this topic, Google found that other brothers have already solved this kind of problem, such as here and here, you can compare reference.


--------------------------------------------------------------------------------

Update, thanks to the Sheneyan brothers reminders, there is another way is through the Closure (closure) to implement, the code is as follows:

var Obj = function (msg) ... {
this.msg = msg;
This.shout = function () ... {
alert (this.msg);
This.waitandshout ();
}

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.