Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/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中的异步调用_Javascript - Fatal编程技术网

javascript中的异步调用

javascript中的异步调用,javascript,Javascript,我正在执行一个函数,我想暂停执行一段时间 function a(){ } //here some code --- I am using result of a function here... 谁能帮我。。。如何使用异步调用 setTimeout(function(){ // do some work after the timeout pause }, 1500); 它在执行包含的函数之前等待了1500毫秒。使用 setTimeout(function(){ // do so

我正在执行一个函数,我想暂停执行一段时间

function a(){

}

//here some code --- I am using result of a function here...
谁能帮我。。。如何使用异步调用

setTimeout(function(){
  // do some work after the timeout pause
}, 1500);
它在执行包含的函数之前等待了1500毫秒。

使用

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

它在执行包含的函数之前等待1500毫秒。

不确定您的意思-要使用函数的结果,请将其分配给变量:

var r = a();

//....some code...

//using result of a function here

alert("result of a(): " + r);
在使用AJAX或类似工具的情况下,请使用提供给您的回调函数,例如jQuery:

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

不确定您的意思-要使用函数的结果,请将其指定给变量:

var r = a();

//....some code...

//using result of a function here

alert("result of a(): " + r);
在使用AJAX或类似工具的情况下,请使用提供给您的回调函数,例如jQuery:

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

您的函数应该接受一个回调参数,它将在完成其操作后执行该参数。比如:

function a(callback) {
    // do something

    if (callback) {
        callback();
    }
}
那么你可以这样称呼它:

a(function () {
    alert("finished");
});

您的函数应该接受一个回调参数,它将在完成其操作后执行该参数。比如:

function a(callback) {
    // do something

    if (callback) {
        callback();
    }
}
那么你可以这样称呼它:

a(function () {
    alert("finished");
});

您可以使用setTimeout(“,function(),time);这其中的哪一部分是异步的?以及暂停?您可以使用setTimeout(“,function(),time);这其中的哪一部分是异步的?停顿呢?