Problems and workarounds for using settimeout and setinterval in Requirejs

Source: Internet
Author: User

The timing-related APIs in JavaScript have settimeout () and SetInterval (), which are 2 functions different, but are used the same way.

The following wording in JavaScript is equivalent to defining a confined space in which the variables and functions defined are private and accessible only within the module.

(function () {var msg = "MSG";}) ();//Error alert (MSG);

I used to write JavaScript, all using settimeout ("Say (' aty ');", 1000); this way, because say is a global function, this writing works correctly. A recent project used the REQUIREJS framework, which required us to write JavaScript in a modular way. Using setTimeout in the previous way will not work.

(function () {function say (msg) {alert (msg);} 1th Way//settimeout ("Say (' aty ');", 1000);//2nd Way//settimeout (Say ("Aty"), 1000);//3rd Way SetInterval (function () {say ("Aty");},1000);}) ();


The first way will be error, because the Say function is only visible inside the module,settimeout invisible;

The second way: code will be executed immediately, did not reach the setTimeout delay effect;

The third way: through this anonymous function call, can meet our needs, that is, solve the problem of delay, but also solve the problem of variable visible domain.


Problems and workarounds for using settimeout and setinterval in Requirejs

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.