使用值数组重复一个函数 [英] Repeating a function using array of values
本文介绍了使用值数组重复一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我有以下js函数:
So, I have the following js function:
var name_list = names; //mike,steve,sean,roger
jQuery.ajax({
type: "GET",
url: custom.ajax_url,
dataType: 'html',
data: ({ action: 'some_function', name_list:name_list}),
success: function(data){
alert('success')
});
这里有一个变量,用于保存值的数组(用逗号分别表示"mike,steve,sean,roger").
Here there is variable that hold an array of values ("mike,steve,sean,roger" separately by a comma).
现在,我想使用第一个值调用ajax(name_list = mike),一旦完成,我想使用第二个值(name_list = steve)重复相同的ajax调用.
Now, I want to use the first value to call an ajax (name_list = mike), and once it is done, I want to repeat the same ajax call with the second value (name_list = steve).
这是我的问题.
如何在变量中使用单个值,并且仅在函数(ajax调用)成功后才使用后续值?
How do I use individual value in the variable and only use the subsequent value when the function (ajax call) is successful?
谢谢!
推荐答案
这是一种实现方法:
var name_list = ['mike','steve','sean','roger'];
function recursive(list, done) {
list = list.slice();
(function next() {
var name = list.shift();
jQuery.ajax({
type: "GET",
url: 'url',
dataType: 'html',
data: ({ action: 'some_function', name : name }),
success: function(data) {
console.log('success', name);
}
}).then(list.length ? next : done);
}());
}
recursive(name_list, function() {
console.log('all done');
});
这篇关于使用值数组重复一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文