gpt4 book ai didi

javascript - 函数完成后如何等待(2秒)?

转载 作者:行者123 更新时间:2023-11-28 19:08:40 24 4
gpt4 key购买 nike

我有一个正在执行的javascript函数,执行后我想等待2秒。在 Javascript 中是否可能。

我的问题不同。我想在函数执行或完成执行后等待,而不是等到函数执行为止。

Javascript函数

function ajax_closeCall(onDone) {
// alert("Close Call invoked.");
closeCall_onDone = onDone;
var closeCallUrl = soapUrl + "?action=closeCall&parentSessionId=" + parentSessionId;
closeCall_http_request = getNewHttpRequest('text/plain');
closeCall_http_request.onreadystatechange = callback_ajax_closeCall;
// http_request.open("POST", soapUrl, true);
closeCall_http_request.open("GET", closeCallUrl, true);
closeCall_http_request.send(null);
}


function callback_ajax_closeCall() {
if (closeCall_http_request.readyState != 4) {
return;
}

if (closeCall_http_request.status == 200) {
if (closeCall_onDone) {
closeCall_onDone();
}
stopMonitorCallState();
ajax_getCallState();
} else {
// there was a problem with the request,
// for example the response may be a 404 (Not Found)
// or 500 (Internal Server Error) response codes
alert(getLabel("cmmm_error_closecallfailed"));
}
}

上述函数执行后,等待2秒。如何实现这个场景。

最佳答案

将代码包装在 setTimeout 中:

setTimeout(function() {
// do your thing!
}, 2000);

关于javascript - 函数完成后如何等待(2秒)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31157129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com