Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 功能完成后如何等待(2秒)?_Javascript_Ajax - Fatal编程技术网

Javascript 功能完成后如何等待(2秒)?

Javascript 功能完成后如何等待(2秒)?,javascript,ajax,Javascript,Ajax,我有一个javascript函数可以执行,执行后我想等待2秒钟。是否可以用Javascript实现 我的问题不同。我希望在函数执行或完成其执行后等待,而不是等到函数执行。 Javascript函数 function ajax_closeCall(onDone) { // alert("Close Call invoked."); closeCall_onDone = onDone; var closeCallUrl = soapUrl + "?action=closeCa

我有一个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);

将代码包装在setTimeout中:

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

setTimeout为您提供异步等待时间。为了一个功能。 如果你想让一切停止两秒钟。您可以使用以下简单的解决方案:

var date = new Date();var i; 
for (i = date.getTime(); i<= date.getTime() + 2000; i = (new Date()).getTime()){/*Do Nothing*/}
var-date=新日期();var i;

for(i=date.getTime();isetTimeout为函数提供异步等待时间。 如果要将所有操作暂停两秒钟,可以使用以下简单的解决方案:

var date = new Date();var i; 
for (i = date.getTime(); i<= date.getTime() + 2000; i = (new Date()).getTime()){/*Do Nothing*/}
var date=new date();var i;

对于(i=date.getTime();i有setTimeout函数

setTimeout(function,milliseconds,param1,param2,...)
也可以使用setInterval函数
setInterval(函数,毫秒);

有setTimeout函数

setTimeout(function,milliseconds,param1,param2,...)
也可以使用setInterval函数
setInterval(函数,毫秒);

您可以使用setInterval

setInterval(函数(){
//写下你想在2秒钟后调用的函数

},2000);
您可以使用setInterval

setInterval(函数(){
//写下你想在2秒钟后调用的函数
},2000);
试试这个

调用函数,然后设置超时

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()
{
}
试试这个

调用函数,然后设置超时

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(function(){//wait for 2 secs,do nothing},2000);setTimeout()方法在指定的毫秒数之后调用函数或计算表达式。还有setInterval()方法将等待指定的毫秒数,然后执行指定的函数,并且它将继续执行该函数,每给定的时间间隔执行一次。但我的问题是在函数执行后必须等待。@user958324您正在等待的函数是什么?在这两个函数中的哪一个之后需要“暂停”使用setTimeout(function(){//wait for 2 secs,do nothing},2000);setTimeout()方法在指定的毫秒数后调用函数或计算表达式。还有setInterval()方法将等待指定的毫秒数,然后执行指定的函数,并且它将继续执行该函数,每给定的时间间隔执行一次。但我的问题是我必须在函数执行后等待。@user958324您正在等待的函数是什么?setTimeout()方法在指定的毫秒数后调用函数或对表达式求值。我说得对吗?正确,因此您希望让代码在第一个函数运行后运行setTimeout,并让它在setTimeout循环中执行下一个逻辑。我认为行为在不同的浏览器中会发生变化,并且在应用时具有随机行为tTimeout在setTimeout中无事可做。例如,有时它在执行函数后会等待2秒钟。setTimeout()方法在指定的毫秒数后调用函数或对表达式求值。我说得对吗?正确,因此您希望让代码在第一个函数运行后运行setTimeout,并让它在setTimeout循环中执行下一个逻辑。我认为行为在不同的浏览器中会发生变化,并且在应用时具有随机行为tTimeout在setTimeout中无事可做。例如,有时在执行函数后会等待2秒。他只想在两个函数完成工作后等待2秒。他不想在每2秒后调用函数。她只想在两个函数完成工作后等待2秒。他不想在ev后调用函数每2秒