功能完成后如何等待(2秒)
How to wait (for 2 seconds) after the completion of function?
我有一个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);
您可以使用setInterval
setInterval(function(){
// write down your function that would you want to call after 2 seconds
}, 2000);
setTimeout为您提供异步等待时间。对于函数。如果你想让一切暂停两秒钟。您可以使用以下琐碎的解决方案:
var date = new Date();var i;
for (i = date.getTime(); i<= date.getTime() + 2000; i = (new Date()).getTime()){/*Do Nothing*/}
试试这个
调用一个函数,然后设置TimeOut
function someFunction() //caller
{
one(); //call function one which will call second function from it
setTimeout(function()
{
//wait for 2 secs, do nothing
}, 2000);
}
// two functions after which you want to wait for 2 secs
function one()
{
two(); //it will call the second function
}
function two()
{
}
有setTimeout函数
setTimeout(function,milliseconds,param1,param2,...)
还可以使用setInterval函数setInterval(函数,毫秒);
相关文章:
- 每隔X秒显示一次警报框,但请先等待Y秒
- 如何使用 JQuery 添加延迟?(让它等待 5 秒再继续)
- iframe 加载事件中的 setTimeout 似乎没有像预期的那样等待 x 秒
- 实现等待元素存在 N 秒,然后使用 JavaScript 和 jQuery lib 与元素交互
- 使用javascript/jQuery,等待3秒钟点击,然后继续
- 如何用等待100秒的javascript刷新页面
- JavaScript:等待n秒,然后继续while循环
- 在一个事件中,立即启动一个函数,但等待x秒,直到它能够再次运行
- PHP计时器等待30秒,然后运行一个命令
- 使用 JavaScript 等待 X 秒后进行页面重定向
- 显示图像,等待10秒并加载页面内容
- JWPlayer在完成视频后等待5秒做一个动作
- 编辑此工具提示实现,使工具提示等待x秒隐藏
- 在onkeyup执行AJAX请求前等待1秒
- 等待5秒后下载按钮出现
- 等待5秒并查看文本
- 功能完成后如何等待(2秒)
- 延迟或等待3秒,然后删除类
- 等待页面加载或等待X秒后再加载播放器
- 如何使代码在单击按钮后等待 X 秒,然后再继续