JavaScript-Implemented Timers

Source: Internet
Author: User

Resolves the following issues:

1 implementing parameters and changing scopes

2 Guaranteed timing trigger, trigger interval.

Call Mode:

1 Method: The methods to be executed use a parameter of paramters [parameter 1, parameter 2]

Paramters: Passed parameters, form [parameter 1, parameter 2]

Milliseconds: Execution interval 1000 for 1 seconds

Scope: Represents a scope

GLOBAL. Timer.setinterval(method, Paramters, Milliseconds,scope);

GLOBAL. Timer.settimeout(method, parameters, Milliseconds,scope);


1Global.namespace ("Timer");2GLOBAL. Timer._calltimeoutmethod =function(method, Parameters,scope) {3     return function() {4         if(typeof(parameters) = = "Undefined" | |typeof(parameters) = = "Null") {5             if(typeof(scope) = = "Undefined" | |typeof(scope) = = "Null")6             {7 method ();8}Else9             {Ten method.apply (scope); One             } A}Else { -             if(typeof(scope) = = "Undefined" | |typeof(scope) = = "Null") -             { the method (parameters); -}Else -             { - method.apply (scope,parameters); +             } -         } +     } A }; atGLOBAL. Timer._callintervalmethod =function(method, parameters, Milliseconds,scope) { -     return function() { -         if(typeof(parameters) = = "Null") { -             if(typeof(scope) = = "Undefined" | |typeof(scope) = = "Null") -             { - method ();} in             Else -             { to method.apply (scope); +             } -}Else { the         if(typeof(scope) = = "Undefined" | |typeof(scope) = = "Null") *             { $ method (parameters);}Panax Notoginseng             Else -             { the method.apply (scope,parameters); +             } A         } the window.settimeout (Arguments.callee, milliseconds); +     } - }; $ /** $ * Call function parameter method requires an array as a parameter -  */ -GLOBAL. Timer.settimeout =function(method, parameters, Milliseconds,scope) { the     if(typeof(parameters) = = "Null") { -         if(typeof(scope) = = "Undefined")Wuyi         { the         returnWindow.settimeout (GLOBAL. Timer._calltimeoutmethod (method), - milliseconds); Wu}Else -                 { About                   returnWindow.settimeout (GLOBAL. Timer._calltimeoutmethod (method,NULL, scope), $ milliseconds); -                 } -}Else { -         if(typeof(scope) = = "Undefined") A         { +         returnWindow.settimeout (GLOBAL. Timer._calltimeoutmethod (method, the parameters), milliseconds); -}Else $                 { the                 returnWindow.settimeout (GLOBAL. Timer._calltimeoutmethod (method, the parameters,scope), milliseconds); the                 } the     } - }; in  theGLOBAL. Timer.cleartimeout =function(Timerid) { the     returnwindow.cleartimeout (Timerid); About }; theGLOBAL. Timer.clearinterval =function(timerintervalid) { the     returnGLOBAL. Timer.cleartimeout (timerintervalid); the }; + /** - * Secure interval execution with settimeout implementation the  */BayiGLOBAL. Timer.setinterval =function(method, Paramters, Milliseconds,scope) { the     if(typeof(parameters) = = "Null") { the         if(typeof(scope) = = "Undefined") -         { -           returnWindow.settimeout (GLOBAL. Timer._callintervalmethod (method,NULL, the milliseconds), milliseconds); the}Else the         { the         returnWindow.settimeout (GLOBAL. Timer._callintervalmethod (method,NULL, - milliseconds,scope), milliseconds); the                 } the}Else { the         if(typeof(scope) = = "Undefined")94         { the         returnWindow.settimeout (GLOBAL. Timer._callintervalmethod (method, the parameters, milliseconds), milliseconds); the}Else98                 { About                 returnWindow.settimeout (GLOBAL. Timer._callintervalmethod (method,NULL, - milliseconds,scope), milliseconds);101                 }102     }103};

JavaScript-Implemented Timers

Related Article

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.