Reputation: 553
I having some trouble figuring this problem out where I have one button which does a mass clicking of other buttons on a page. Though the issue is I don't want to activate all the buttons all at once and basically time them out until the other buttons finish their actions.
Page.MassCheckStats = function(){
$('[data-field="button_update_stats"]').each(function(){
$(this).click();
});
}
How can I get the above code to click 3 buttons at a time before going on to the other buttons.
Upvotes: 1
Views: 430
Reputation: 11588
You can use recursive timeouts like this:
(sorry, i forgot it was three buttons at a time);
Page.MassCheckStats = function(button){
var button = typeof button == 'undefined'?$('[data-field="button_update_stats"]')[0] : button;
for(var i = 0; i < 3; i++){
$(button).click();
if($(button).next().length == 0) return;
button = $(button).next();
}
setTimeout(function(){
Page.MassCheckStats($(button));
}, 1000);
}
where 1000 = 1000 miliseconds = 1 second
and here's the fiddle for it: http://jsfiddle.net/andrepadez/cR4tP/2/
the firs line inside the function is a trenary operator. it means that if you do not pass any parameter calling the function (the first time it is called), typeof button == 'undefined'
, so it will assign to var button
the first element selected by the jQuery selector - $('[data-field="button_update_stats"]')[0]
. When i call it again from the timeouts, i am passing the current $(button).next()
so it will assume what was passed in the parameter.
Upvotes: 1
Reputation: 25753
This sounds incredibly hacky, but perhaps you could attach yet another click handler.
$(document).ready(function(){
$buttons = $('[data-field="button_update_stats"]');
// attach your original events here
for( var i = 0; i < $buttons.length-1; i++ ) {
$button = $($buttons.get(i));
$button.click(function(){$($buttons.get(i+1)).click();});
}
}
// to start the chain
$($('[data-field="button_update_stats"]').get(0).click();
This code is completely ad-hoc and wasn't tested
Upvotes: 1
Reputation: 1583
instead of worrying about the buttons you should just call the functions assigned to each of the buttons click events
or if its the case of a certain button needing to do what the other three buttons do first, its function should contain those 3 functions
Upvotes: 2