The error in the title of the post came from jQuery version 1.10.2, line 637
我有一个模态框,在单击带有一些文本框的按钮时弹出,当单击模态框内的按钮时,文本框中的信息通过 AJAX 添加到数据库中。为了使页面更加用户友好,我添加了一个 setTimeout 函数来暂停模式的隐藏,以便用户可以看到数据已添加到数据库的验证消息。我的代码块 1 将记录添加到数据库中,但 setTimeout 调用无法正常工作:
function insert(data) {
data = JSON.stringify(data);
$.ajax({
type: "POST",
url: "../Service.asmx/InsertPerson",
dataType: "json",
contentType: "application/json",
data: data,
//record gets added to the database
//something about the setTimeout function
//that gives the error in the title
success: function () {
console.log('success before setTimeout');
var successMessage = $('<div>').text('Successfully added to the database...').css('color', 'green');
$('.modal-body').append(successMessage);
//*******this function doesn't run
window.setTimeout(function () {
$('#contact').modal('hide');
$('.modal-body input').each(function () {
$(this).val('');
}, 1000);
});
}
});
}
我使用代码修复了它:(成功功能是我们这里需要注意的)
function insert(data) {
data = JSON.stringify(data);
$.ajax({
type: "POST",
url: "../Service.asmx/InsertPerson",
dataType: "json",
contentType: "application/json",
data: data,
//record gets added to the database
success: function () {
console.log('success before setTimeout');
var successMessage = $('<div>').text('Successfully added to the database...').css('color', 'green');
$('.modal-body').append(successMessage);
window.setTimeout(function () {
$('.modal-body input').each(function () {
$(this).val('');
});
$('#contact').modal('hide');
}, 1000);
}
});
}
我看到我在第一个块中没有关闭该each
函数,我在第二个块中修复了它,这就是它起作用的原因,但为了将来参考,这个错误在这种情况下真正意味着什么?