Transfer objects in the window. setTimeout Method

Source: Internet
Author: User

Its syntax is:

Window. setTimeout (expr, msec)

Expr is the execution string and will run as js after msec milliseconds. I discovered yesterday that expr can also be a function. With this feature, you can transfer objects.
The following code implements the function to transmit the object p in function foo1 to function foo2.
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]


[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Currently, many frameworks directly adopt a multi-vote event (its implementation principle is not complicated). A multi-vote event itself is actually a standard function, but it generally has the following methods.

MuEvent. add = function (func ){...}

MuEvent. addMethod = function (instance, func ){...}

When the first method is used, when the func event handler function is activated, the current instance of MuEvent is the this object; the second method uses the passed instance as the this object.

Therefore, for setTimeout, our traditional method should use it to activate the method as follows:

----------
Function doTimer (){
Obj1.call ();
Obj2.call ();
}
SetTimeout (doTimer, 1000 );

The code for using multiple-vote events can be as follows:

----------
Var e = new MuEvent ();
E. addMethod (obj1, obj1.call );
E. addMethod (obj2, obj2.call );

SetTimeout (e, 1000 );
----------

Of course, you want to write a COOL point, which can be like this:

----------

SetTimeout (function (){

Return new MuEvent (obj1, obj1.call, obj2, obj2.call );

} (), 1000 );
----------

As a little introduction, Qomo is implemented in this form of MuEvent. Most frameworks like Atlas adopt similar methods.

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.