When writing a JS script, you often use some spelling functions, such as calling SetTimeout
Copy Code code as follows:
var msgalert= "Test";
function Testalert (msg)
{
Alert (msg)
}
$ (document). Ready (function () {
$ ("#btnCancel"). Click (function (e) {
settimeout ("Testalert" ("+msgalert+") ", 1000);
});
})
Check for a long time, why can not play out the dialog box. Check for a long time to find that the original is less than a pair of single quotes
Copy Code code as follows:
$ (document). Ready (function () {
$ ("#btnCancel"). Click (function (e) {
settimeout ("Testalert" (' +msgalert+ ")", 1000);
});
})
This kind of writing error prone, not easy to check out the error, if the use of closures can be completely avoided, rewrite the following
Copy Code code as follows:
var msgalert= "Test";
function Dalayalert (msg, time) {
SetTimeout (
Testalert (msg),
Time
);
}
function Testalert (msg)
{
Alert (msg)
}
$ (document). Ready (function () {
$ ("#btnCancel"). Click (function (e) {
Dalayalert (msgalert,1000)
});
})
Because of the use of closures, but also a lot easier to check the error is also very easy