I have looked at many Posts regarding setInterval
and none of them have the answer I am looking for or what I am trying to achieve what I want.
I am looking for one setInterval
function with 2 clicks. The first click is separated by 2sec from the 2nd click. It only runs once What I have at the moment is a workaround using two functions, it works.. but I would rather have them in one function rather than two functions.
The first click is immediate runs in one seconds (1000)it closes the open modal. Then the next click is after 2 seconds(2000) it removes a clone. Below is what I am using at the moment with in my $each
. Thanks for your help.
$.each(deleteCard, function( i, person ) { setTimeout(function() { $("#"+person.id).find('.close-modal').click(); }, 1000); setTimeout(function() { $("#"+person.id).find('.card').remove(); }, 2000); });
Advertisement
Answer
I don’t think setInterval
will help here. It’s a poor choice for only repeating something twice (your original code also had an irregular delay, something setInterval
doesn’t do well).
I’d uses promises and write a wait function as a general utility you can tuck away in a helper file somewhere:
const wait = ms => new Promise(resolve => setTimeout(resolve, ms)); $.each(deleteCard, async function (i, person) { await wait(1000); $("#" + person.id).find('.close-modal').click(); await wait(1000); $("#" + person.id).find('.card').remove(); });