AJAX——AJAX請求遞歸

問題描述

需要發送同一個請求,但是AJAX異步機制的存在,在for/while/do..while使用AJAX會出現問題。

問題分析

封裝函數,遞歸調用

解決方案

currentIndex = 0;
function getImg(){
	if(currentIndex>=items.length){ 
		return;
	}
	var url = item[url];
	console.log(i);
	$.ajax({
		type: 'get',
		url: url,
		dataType: "json",
		async: false,
		cache: true,
		success: function(json){
			currentIndex++;
			console.log("test");
			var img = json.img;
 
			getImg();
		},
		error: function(data){
			console.log("error...");
			currentIndex++;
			getImg();
		}
	});
}

參考文章

https://blog.csdn.net/chdyiboke/article/details/81914257

https://www.runoob.com/ajax/ajax-examples.html

https://blog.csdn.net/yuan882696yan/article/details/50296821

http://www.oschina.net/code/snippet_574558_13233

https://zhidao.baidu.com/question/245639610.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章