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