1

I have a list of buttons that have an onclick event inline

 <button id='someId' class='clicker' onclick='doSomething(someId)'>Clicky<button>
 <button id='someId' class='clicker' onclick='doSomething(someId)'>Clicky<button>
 <button id='someId' class='clicker' onclick='doSomething(someId)'>Clicky<button>
 <button id='someId' class='clicker' onclick='doSomething(someId)'>Clicky<button>

The id of the button is the same as the variable passed to the doSomething function.

Is there a way, using jQuery to fire the onclick event for each button with a delay of 200 ms between clicks?

something like

$('.clicker').each(function(){
    // wait 200 ms
    // then fire event
});

the above function just waits 200ms and then fires all events, rather than spacing them out. any way to add delay between events?

3
  • I would assume that you have to do something with .delay(200) and create a queue somehow. Commented Mar 29, 2013 at 16:34
  • 6
    Why are you so intent to clicking the buttons? Couldn't you, for instance, just call whatever function is attached to the click event? Do you need an actual click event, with spoofed mouse position and all that nastiness? Commented Mar 29, 2013 at 16:34
  • the reason for the click function would be that the above structure is much more complicated. Each of the buttons above is generated conditionally and may or may not have a certain Id or class. This way I can just check the condition of having a class or having an id and click all buttons that meet that condition. Commented Mar 29, 2013 at 16:54

1 Answer 1

3
$('.clicker').each(function (ind, elem) {
    window.setTimeout(function () {
        $(elem).click()
    }, 200 * ind);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.