user1958502
user1958502

Reputation:

JavaScript jQuery trigger click every second

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?

Upvotes: 1

Views: 1852

Answers (1)

Vinay
Vinay

Reputation: 6322

$('.clicker').each(function (ind, elem) {
    window.setTimeout(function () {
        $(elem).click()
    }, 200 * ind);
});

Upvotes: 3

Related Questions