1

I have javascript function which executes and after the execution i want to wait for 2 seconds. Is it possible in Javascript or not.

My Question is different. I want to wait after function gets executed or completed its execution not for till the function executes.

Javascript function

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"));
    }
}

After the above function executes, wait for 2 seconds. How to achieve this scenario.

Beutiful World
  • 69
  • 1
  • 3
  • 12

5 Answers5

5

You wrap the code in a setTimeout:

setTimeout(function() {
    // do your thing!
}, 2000);
Stephen Wright
  • 2,908
  • 4
  • 19
  • 28
  • The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. Am i Right ? – Beutiful World Jul 01 '15 at 09:42
  • Correct, so you would want to make your code run setTimeout after the first function runs, and have it execute the next logic within the setTimeout loop. – Stephen Wright Jul 01 '15 at 09:48
  • I think the behavior changes in different browsers and has random behavior when applied setTimeout with nothing to do in setTimeout. i.e. sometimes it wait for 2 seconds after executing the function. – Beutiful World Jul 01 '15 at 10:37
0

setTimeout gives you asynchronous wait time. for a function. If you want to halt everything for two second. You can use the following trivial solution :

var date = new Date();var i; 
for (i = date.getTime(); i<= date.getTime() + 2000; i = (new Date()).getTime()){/*Do Nothing*/}
binariedMe
  • 4,309
  • 1
  • 18
  • 34
0

there is setTimeout function

setTimeout(function,milliseconds,param1,param2,...)

and you could use also setInterval function also setInterval(function, milliseconds);

Regius
  • 70
  • 1
  • 9
0

you can use setInterval

setInterval(function(){
  // write down your function that would you want to call after 2 seconds
}, 2000);
Himesh Aadeshara
  • 2,114
  • 16
  • 26
  • 1
    he just want to wait for 2 secs aafter the two functions finishes work. he don't want to call the function after every 2 secs – Newinjava Jul 01 '15 at 09:55
0

Try this

call a function and then setTimeOut

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()
{
}
Newinjava
  • 972
  • 1
  • 12
  • 19