Repeating A Function Using Array Of Values
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: ({ ac
Solution 1:
This is one way of doing it:
var name_list = ['mike','steve','sean','roger'];
functionrecursive(list, done) {
list = list.slice();
(functionnext() {
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');
});
Solution 2:
simply try to make a function with ajax you want and pass values and on success run the ajax again till it reached to the last length of your array
var name_list = names.split(','); //mike,steve,sean,rogervar repeat_Ajax = function(i){
jQuery.ajax({
type: "GET",
url: custom.ajax_url,
dataType: 'html',
data: { action: 'some_function', name_list:name_list[i]}, // no need for `( )` heresuccess: function(data){
if(i !== name_list.length){
repeat_Ajax(i+1);
alert('success');
}
});
}
repeat_Ajax(0);
Untested code but I hope it work with you
Solution 3:
I added a button to demonstrate that it works, use the console to observe.
<!doctype html><html><head><metacharset="utf-8"><title>SerialArrayFeed</title></head><body><buttonid="btn">Feed</button><scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script><script>
jQuery.ajax({
type: "GET",
url: "_custom.ajax.url",
dataType: "html",
data: ({
action: serialArrayFeed,
names: arr
}),
success: function(data) {
if (data != undefined) {
alert('success');
}
}
});
var arr = ['mike', 'steve', 'sean', 'roger'];
var qty = arr.length;
functionserialArrayFeed(arr) {
for (var i = 0; i < qty; i++) {
var next = arr.pop(i);
break;
return next;
}
console.log('arr: ' + arr + ' next: ' + next);
}
var btn = document.getElementById('btn');
btn.addEventListener('click', function(event) {
serialArrayFeed(arr);
}, false);
</script></body></html>
Post a Comment for "Repeating A Function Using Array Of Values"