Home > Web Front-end > JS Tutorial > body text

Use closures to simply encapsulate setTimeout to avoid errors_javascript tips

WBOY
Release: 2016-05-16 17:29:17
Original
1073 people have browsed it

When writing js scripts, some spelling functions are often used, such as calling setTimeout

Copy code The code is as follows:

var msgalert="test";
function TestAlert(msg)
{
alert(msg)
}

$(document).ready( function () {
$("#btnCancel").click(function (e) {
setTimeout("TestAlert(" msgalert ")",1000);
});
})

After checking for a long time, why the dialog box does not pop up. After checking for a long time, I discovered that a pair of single quotes
was missing. Copy the code The code is as follows:

$(document).ready(function () {
$("#btnCancel").click(function (e) {
setTimeout("TestAlert('" msgalert "')",1000 ; 🎜>


Copy code


The code is as follows:);
}
function TestAlert(msg)
{
alert(msg)
}

$(document).ready(function () {
$("#btnCancel").click(function (e) {
dalayAlert(msgalert,1000)
}) ;
})


Due to the use of closures, it is much simpler and it is easy to check for errors.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!