gpt4 book ai didi

javascript, $.ajax, 变量名

转载 作者:行者123 更新时间:2023-11-29 17:56:17 27 4
gpt4 key购买 nike

我正在尝试迭代一个数组并使用 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 = "";
}
})

我遇到的问题是 name 没有进入 success 函数。我以为 js 一直向上移动以查找当前范围内不存在的变量?如果我尝试使用 console.log 名称,我会得到 name 变量的未定义!范围大师我做错了什么?

最佳答案

您可以使用.queue()$.map() 来维护name 的范围。此外,将 status 数组更改为具有属性 status 的对象,其中值是一个数组,以防止可能与 Person 的 this.status 发生冲突 对象。

注意,当所有排队函数都在 queueName,即 "status" 已被调用,queueName .length0

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/

关于javascript, $.ajax, 变量名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38758499/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com