JS中怎样等待异步调用完成再执行后面的同步操作?

我在计时器中给一个全局变量赋值,计时器后有一个同步操作需要用到,但实际情况是
先执行的是同步操作再执行计时器操作,全局变量最后才被赋值。有什么办法解决这一
一问题吗?

函数回调,在你要执行的操作中,用函数回调执行计时操作

计时器执行的函数里面调用同步操作

具体的怎么操作,能给我段代码吗?或者推荐一些学习的网址和资料,谢谢你

用 async:false

比如:

$.ajax({
url: _data_url,
dataType: "json",
async: false,
data: "",
beforeSend: function () {
},
success: function (data) {
//do things what you want!
}
});

以下是官方关于async的说明:link:----http://api.jquery.com/jquery.ajax/

async (default: true)
Type: Boolean
By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done().

可以用ajax,请求成功在回调里面执行你要进行的操作

利用AJAX同步请求刷新

异步请求就是在你发送ajax 的时候不用等 响应流回来就可以执行下面的代码,如果要等数据返回再执行的代码,必须得放在回调函数中,别无他法