我知道"do-while“至少会运行一次,而"while”必须满足一个条件才能运行。
这是do-while的正确用法吗?
do{
var ajaxresponse = make an ajax call and assign the response to the variable.
while( ajaxresponse.length == <some number>);发布于 2016-03-16 12:30:52
如果要使AJAX调用同步,这将是有效的。
例如。
$.ajax({url:"url",..., async: false});但是,这完全违背了AJAX是异步的这一点,所以应该避免。
发布于 2016-03-16 13:06:21
对于在响应数据上迭代和执行操作,最好在回调函数中执行。因为AJAX是异步的。下面的代码使用Jquery处理Ajax请求。
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( ajaxresponse ) {
do{
//operations
}while(ajaxresponse.length == <some number> );
});发布于 2016-03-16 12:37:21
编号:
当JavaScript在web客户端中运行时,它会同步运行-这意味着一次只能有一个JavaScript线程执行。
因此,如果函数调用要执行的异步操作(如XmlHttpRequest.send或setTimeout),则该操作由JavaScript运行时外部的浏览器通过WebAPI在单独的线程中处理,然后该操作的回调将排队,并在JavaScript运行时空闲时执行。
在你的代码中:
do{
var ajaxresponse = make an ajax call and assign the response to the variable.
while( ajaxresponse.length == <some number>);在包含do/while循环的函数完成之前,ajaxresponse不会返回,所以当您的while条件检查ajaxresponse时,将没有要检查的值。
https://stackoverflow.com/questions/36026766
复制相似问题