“Quick and dirty? Inline Promise does the job”
Yep— @kumari_babitaa version is clean and reusable. But if you’re just experimenting or writing something short and sweet, here’s a no-frills way to make javascript wait 5 seconds:
async function stateChange(newState) {
await new Promise(resolve => setTimeout(resolve, 5000));
if (newState == -1) {
alert('VIDEO HAS STOPPED');
}
}
Just remember to call stateChange() from an async function or top-level async context. No need to define a separate delay() function if you’re just doing a one-off delay.