0

I am executing a function and i want to pause execution for some time.

function a(){

}

//here some code --- I am using result of a function here...

Can anyone help me... how to use asynchronous call??

2
  • you can use setTimeout("",function(),time); Commented Apr 3, 2012 at 7:02
  • 2
    what part of this is async? and the pause? Commented Apr 3, 2012 at 7:03

3 Answers 3

4

Your function should accept a callback parameter that it would execute after doing its thing. So something like:

function a(callback) {
    // do something

    if (callback) {
        callback();
    }
}

Then you would call it like:

a(function () {
    alert("finished");
});
Sign up to request clarification or add additional context in comments.

Comments

0

How about just using

setTimeout(function(){
  // do some work after the timeout pause
}, 1500);

It is waiting for 1500 milliseconds before executing the function contained.

Comments

0

Not sure what you mean - to use result of a function, assign it to a variable:

var r = a();

//....some code...

//using result of a function here

alert("result of a(): " + r);

In case of AJAX or something similar use the callback function provided to you, for example with jQuery:

$.post("myajaxpage", function() {
    //callback function - executed after request is finished
    var r = a();
    alert("result of a(): " + r);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.