JSFiddle - React, Tailwind, and code Playground
JavaScript
var intervalID = 0;
var wait =
ms => new Promise(
r => setTimeout(r, ms)
);
var repeat =
(ms, func) => new Promise(
r => (
intervalID = setInterval(func, ms),
wait(ms).then(r)
)
);
var myfunction =
() => new Promise(
r => r(console.log('repeating...'))
);
var stopAfter5Secs =
() => new Promise(
r => r(setTimeout(
() => {
clearInterval(intervalID);
console.log('repeat end')
} , 5000))
);
repeat(1000, () => Promise.all([myfunction()]))
.then(stopAfter5Secs()) // starts timer to end repetitions
.then(console.log('repeat start')); // informs that all actions were started correctly and we are waiting for them to finish