在下面这行代码中
setTimeout(Comet_IrsaliyeBelgeDurum(sGuid, belgeOid), 10000)
不会让Comet函数等待10秒。该函数正在连续运行。
setTimeout参数似乎没有任何效果。
如何让代码等待10秒?
function Comet_IrsaliyeBelgeDurum(sGuid, belgeOid) {
var params = {
sGuid: sGuid,
belgeOid: belgeOid
}
$.ajax({
type: "post",
dataType: "json",
data: params,
url: '/BetonHareketler/H_BetonIrsaliyeBelgeDurum',
success: function (data) {
if (data.isSuccess) {
if (data.entity == 2 || data.entity == 4) {
toastr.success(data.SuccessfullMessage, 'İşlemi Başarılı');
}
else {
toastr.info(data.SuccessfullMessage, 'İşlemi Başarılı');
setTimeout(Comet_IrsaliyeBelgeDurum(sGuid, belgeOid), 10000);
}
}
else {
toastr.error(data.SuccessfullMessage, 'İşlemi Başarısız');
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Bağlantı Hatası. Sayfaya Yenileyin");
window.location.replace(window.location.href);
}
});
}
发布于 2019-07-08 18:01:40
setTimeout
接受一个函数,该函数在延迟过后调用。
setTimeout(Comet_IrsaliyeBelgeDurum(sGuid, belgeOid), 10000);
^---------------------------------------^
this got evaluated
您的代码所做的就是调用Comet_IrsaliyeBelgeDurum
并使用它的返回值作为setTimeout
的“函数”。
您需要做的就是将其封装在另一个函数中,如下所示:
setTimeout(function(){
Comet_IrsaliyeBelgeDurum(sGuid, belgeOid)
}, 10000);
发布于 2019-07-08 18:02:47
问题在于调用setTimeout的方式:
setTimeout(Comet_IrsaliyeBelgeDurum(sGuid, belgeOid), 10000);
Javascript是一种按值传递的语言。这意味着,您传递的所有参数在被提供给函数之前都会进行计算。
这意味着您正在将值Comet_IrsaliyeBelgeDurum(sGuid, belgeOid)
和10000
传递给setTimeout。然后调用函数Comet_IrsaliyeBelgeDurum
。
您要做的是将一个函数(而不是函数的结果)传递给setTimeout
。有关示例,请参阅Joseph的答案。
https://stackoverflow.com/questions/56940285
复制