JS 要发起100个异步请求提交数据,有没有方案能分批次提交,比如每次处理5条,再发起下一批?

JavaScript012

JS 要发起100个异步请求提交数据,有没有方案能分批次提交,比如每次处理5条,再发起下一批?,第1张

用一个全局变量来统计正在发起的请求数量,比如说第一批发起五个异步请求,这个变量就设为5,然后在每个请求的回调函数中不管成功与否均把这个变量-1,如果变量为0,就说明5个请求均调用完毕了,这样就可以继续发起下一批5个请求,并把变量重新设为5……以此类推,直到全部完成

ajax({

url: "", //请求地址

type: "POST", //请求方式

data: { name: "super", age: 20 },//请求参数

dataType: "json",

success: function (response, xml) {

// 此处放成功后执行的代码

},

fail: function (status) {

// 此处放失败后执行的代码

}

})

function ajax(options) {

options = options || {}

options.type = (options.type || "GET").toUpperCase()

options.dataType = options.dataType || "json"

var params = formatParams(options.data)

if (window.XMLHttpRequest) {

var xhr = new XMLHttpRequest()

} else {

var xhr = new ActiveXObject('Microsoft.XMLHTTP')

}

xhr.onreadystatechange = function () {

if (xhr.readyState == 4) {

var status = xhr.status

if (status >= 200 &&status <300) {

options.success &&options.success(xhr.responseText, xhr.responseXML)

} else {

options.fail &&options.fail(status)

}

}

}

if (options.type == "GET") {

xhr.open("GET", options.url + "?" + params, true)

xhr.send(null)

} else if (options.type == "POST") {

xhr.open("POST", options.url, true)

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")

xhr.send(params)

}

}

function formatParams(data) {

var arr = []

for (var name in data) {

arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]))

}

arr.push(("v=" + Math.random()).replace("."))

return arr.join("&")

}

var xmlHttp

function createXMLHttpRequest(){

//Mozilla 浏览器(将XMLHttpRequest对象作为本地浏览器对象来创建)

if(window.XMLHttpRequest){ //Mozilla 浏览器

xmlHttp = new XMLHttpRequest()

}else if(window.ActiveXObject) { //IE浏览器

//IE浏览器(将XMLHttpRequest对象作为ActiveX对象来创建)

try{

xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")

}catch(e){

try {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")

}catch(e){}

}

}

if(xmlHttp == null){

alert("不能创建XMLHttpRequest对象")

return false

}

}

//用于发出异步请求的方法

function sendAsynchronRequest(url,parameter,callback){

createXMLHttpRequest()

if(parameter == null){

//设置一个事件处理器,当XMLHttp状态发生变化,就会出发该事件处理器,由他调用

//callback指定的javascript函数

xmlHttp.onreadystatechange = callback

//设置对拂去其调用的参数(提交的方式,请求的的url,请求的类型(异步请求))

xmlHttp.open("GET",url,true)//true表示发出一个异步的请求。

xmlHttp.send(null)

}else{

xmlHttp.onreadystatechange = callback

xmlHttp.open("POST",url,true)

xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")

xmlHttp.send(parameter)

}

}

//以上代码是通用的方法,接下来是调用以上的方法

function loadPros(title,count,pid,cid,level){

// 调用异步请求方法

url = "。。。。。。。。"

sendAsynchronRequest(url,null,loadCallBack)

}

// 指定回调方法

function loadCallBack(){

try

{

if (xmlHttp.readyState == 4) {

if (xmlHttp.status == 200) {

if(xmlHttp.responseText != null &&xmlHttp.responseText != ""){

var divProid = document.getElementById('videolist')

divProid.innerHTML = xmlHttp.responseText

for(i=0i<leni++)

{

var video_url = document.getElementById("videolist"+i+"").href

if(video_url != undefined &&video_url != null &&video_url != ""){

window.location.href = video_url

}

}

}

}

}

if (xmlHttp.readyState == 1)

{

//alert("正在加载连接对象......")

}

if (xmlHttp.readyState == 2)

{

//alert("连接对象加载完毕。")

}

if (xmlHttp.readyState == 3)

{

//alert("数据获取中......")

}

}

catch (e)

{

//alert(e)

}

}