javascript,$。ajax,變量名
您可以使用.queue(),$.map()以保持范圍name。此外,改變status陣列的具有屬性的對象status,其中值是一個數組,以防止可能出現的沖突this.status的Person對象。
請注意,您也可以連接.promise(/* queueName*/)在執行任務.then()時,在所有排隊的功能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(),返回相同的結果
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/
解決方法我正在嘗試遍歷數組,并為for循環分配變量。所以像這樣:
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 = ''; }})
我遇到的問題是名稱沒有進入成功功能。我以為js會繼續向上查找該變量,如果它在當前作用域中不存在?如果嘗試console.logname,我將無法為name變量定義!示波器大師我在做什么錯?
相關文章:
1. javascript - 為什么這個點擊事件需要點擊兩次才有效果2. javascript - ionic2 input autofocus 電腦成功,iOS手機鍵盤不彈出3. javascript - 百度圖片切換圖片時url會改變,但無刷新,沒用hash,IE8也支持,請問是用了什么技術?4. javascript - SuperSlide.js火狐不兼容怎么回事呢5. javascript - 關于選擇器的簡單小問題6. javascript - 移動端引入js去控制根元素的font-size,但在頁面初始化時會有抖動的情況,請問有別的優化方法么7. javascript - ios返回不執行js怎么解決?8. javascript - jQuery中live事件在移動微信端下沒有效果;代碼如下9. javascript - 使用百度文本編輯器ueditor不顯示樣式問題10. javascript - bootstrap table固定列之后寬度無法對齊怎么解決?
