Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading