javascript,$。ajax,變量名
您可以使用.queue(),$.map()以保持范圍name。此外,改變status陣列的具有屬性的對象status,其中值是一個(gè)數(shù)組,以防止可能出現(xiàn)的沖突this.status的Person對象。
請注意,您也可以連接.promise(/* queueName*/)在執(zhí)行任務(wù).then()時(shí),在所有排隊(duì)的功能queueName,IEG,'status'一直呼吁,queueName.length是0。
function Person(name, status){ this.name = name; this.status = status;}var blob = new Blob([’{'stream':null}’], {type:'application/json'});var url = URL.createObjectURL(blob);// change `status` array reference, e.g., to `arr`var arr = {status:[]};var array = ['bill','bob','carl','ton'];$(arr).queue('status', $.map(array, function(curr) { return function(next) { var name = curr; // do asynchronous stuff $.ajax({url:url, dataType:'json'}) .then(function(data) { if(data.stream == null){ var person = new Person(name, 'dead'); console.log(name, person); arr.status.push(person); } }) .then(next) // call next function in `'status'` queue }})).dequeue('status').promise('status')// do stuff when all functions in `'status'` queue have completed,// `'status'` queue `.length` is `0`.then(function() { // `this` : `arr` as jQuery object // `this[0].status`: array containing objects pushed to `arr.status` console.log(this[0].status); // $(this).prop('status');});<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
jsfiddle https://jsfiddle.net/nnayjckc/2/
您也可以使用$.when(),.apply(),$.map(),返回相同的結(jié)果
function Person(name, status) { this.name = name; this.status = status;}var blob = new Blob([’{'stream':null}’], { type: 'application/json'});var url = URL.createObjectURL(blob);// change `status` array reference, e.g., to `arr`var arr = { status: []};var array = ['bill', 'bob', 'carl', 'ton'];$.when.apply($, $.map(array, function(curr) { var name = curr; return $.ajax({ url: url, dataType: 'json' }) .then(function(data) { if (data.stream == null) {var person = new Person(name, 'dead');console.log(name, person);arr.status.push(person); } })})).then(function() { console.log(arr.status)});<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
jsfiddle https://jsfiddle.net/nnayjckc/3/
解決方法我正在嘗試遍歷數(shù)組,并為for循環(huán)分配變量。所以像這樣:
function Person(name,status){ this.name = name; this.status = status;}var status = [];var array = ['bill','bob','carl','ton'];function exAjax(function(){ for(var i = 0; i < array.length; i++){ var name = array[i]; console.log(name); =====> this gives the correct name $.ajax({ url: xxxxxxx,success: function(data){ if(data.stream === null){ var person = new Person(name,'dead'); console.log(name); =====> return undefined until the last person status.push(person); } } }) name = ''; }})
我遇到的問題是名稱沒有進(jìn)入成功功能。我以為js會(huì)繼續(xù)向上查找該變量,如果它在當(dāng)前作用域中不存在?如果嘗試console.logname,我將無法為name變量定義!示波器大師我在做什么錯(cuò)?
相關(guān)文章:
1. javascript - log4js的使用問題2. javascript - Web微信聊天輸入框解決方案3. javascript - Ajax加載Json時(shí),移動(dòng)端頁面向左上角縮小一截兒,加載完成后才正常顯示,這該如何解決?4. javascript - react 中綁定事件和阻止事件冒泡5. javascript - history.replaceState()無法改變query參數(shù)6. javascript - 音頻加載問題7. javascript - 為什么form表單提交沒有跨域問題,但ajax提交有跨域問題?8. javascript - 為什么這個(gè)點(diǎn)擊事件需要點(diǎn)擊兩次才有效果9. javascript - 移動(dòng)端textarea不能上下滑動(dòng),該怎么解決?10. javascript - es6將類數(shù)組轉(zhuǎn)化成數(shù)組的問題
