javascript中do while循环的用例?
我知道,与必须满足运行条件的“while”相比,“do while”将至少运行一次 这是DoWhile的正确用例吗javascript中do while循环的用例?,javascript,loops,Javascript,Loops,我知道,与必须满足运行条件的“while”相比,“do while”将至少运行一次 这是DoWhile的正确用例吗 do{ var ajaxresponse = make an ajax call and assign the response to the variable. while( ajaxresponse.length == <some number>); do{ var ajaxresponse=进行ajax调用并将响应分配给变量。 而(ajaxsresponse
do{
var ajaxresponse = make an ajax call and assign the response to the variable.
while( ajaxresponse.length == <some number>);
do{
var ajaxresponse=进行ajax调用并将响应分配给变量。
而(ajaxsresponse.length==);
如果要使AJAX调用同步,这将是有效的
前
但是,这完全违背了AJAX是异步的这一点,因此应该避免。否。
当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{
var ajaxresponse=进行ajax调用并将响应分配给变量。
而(ajaxsresponse.length==);
ajaxresponse
在包含do/while循环的函数完成之前不会返回,因此当while条件检查ajaxresponse
时,将没有要检查的值。对于对响应数据进行迭代和执行操作,最好在回调函数中这样做。因为AJAX是异步的。Fo下面的代码将Jquery用于Ajax请求
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( ajaxresponse ) {
do{
//operations
}while(ajaxresponse.length == <some number> );
});
$.ajax({
方法:“张贴”,
url:“some.php”,
数据:{姓名:“约翰”,地点:“波士顿”}
})
.完成(功能(ajaxresponse){
做{
//操作
}而(ajaxsresponse.length==);
});
No.AJAX=异步….^循环=synchronous@FelixKling:do{wait fetch(…);}while(…)
:-P
$.ajax({
method: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
})
.done(function( ajaxresponse ) {
do{
//operations
}while(ajaxresponse.length == <some number> );
});