How to add a delay to a function in a single line for a bookmarklet

Viewed 16

I have an issue, I need to delay a function in a bookmarklet, but im unsure how, since most delaying functions require more than one line of code

javascript:var result = confirm("Attempt to perform function"); if(result) while (true) { alert("function performed!")() } else { alert("Cancelled function."); }

I would like to add a delay between the confirmation of trying to perform a function, and when it actually performs the functions (maybe 3 seconds?) any suggestions? thanks.

(my friend recommended requestAnimationframe, but i am unsure how to implement this as well)

1 Answers

If you just want to execute a function after some delay, you should use setTimeout.

For example, this will execute after three seconds (3000ms):

setTimeout(() => alert("function performed!"), 3000);

Also, if you're building bookmarklets, there are helpful tools out there like this one or this one that will let you write "normal" Javascript and then transform it into a single line for you. Might make your life a little easier.

Related