-1

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 Answers1

0

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.

Donut
  • 110,061
  • 20
  • 134
  • 146
  • when running as `javascript:var result = confirm("Attempt to perform function?"); if(result) while (true) { setTimeout(() => alert("function performed!"), 3000); } else { alert("Cancelled function."); }` it doesn't seem to work – HyperScream Sep 20 '22 at 13:53
  • You don't need `while (true)`. The `setTimeout` call should be the entire body of your `if` statement: `javascript:var result=confirm("Attempt to perform function?");if(result){setTimeout(()=>alert("function performed!"),3000);}else{alert("Cancelled function.");}` – Donut Sep 20 '22 at 13:54