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 - Web微信聊天輸入框解決方案2. javascript - vue過渡效果 css過渡 類名的先后順序3. javascript - es6將類數組轉化成數組的問題4. javascript - react熱加載的一段代碼5. javascript - npm安裝報錯 系統w7 求大神解答6. javascript - 有沒有什么好的圖片懶加載的插件,需要包含監聽頁面滾動高度,然后再加載的功能7. javascript - history.replaceState()無法改變query參數8. javascript - 奇怪的Symbol的問題9. javascript - 這段代碼如何理解?10. javascript - react 中綁定事件和阻止事件冒泡
