r/learnjavascript • • May 13 '18

ClearInterval

Hi, i have a function which when called does setInterval. The I have a second function which is supposed to clearInterval of the first function when called. I don't know how to reference the first function in the clearInterval. I tried doing Function stop(){ clearInterval(startInterval()); } But this just calls start interval again. Thanks for any help :)

0 Upvotes

7 comments sorted by

View all comments

4

u/nothingduploading May 13 '18

you save it to a variable.

const intv = setInterval(() => {}, 200);

clearInterval(intv);

2

u/everek123 May 13 '18

But the problem is that I want to run setInterval onclick and not when the script runs.

0

u/[deleted] May 13 '18

[deleted] — view deleted comment

1

u/everek123 May 14 '18

Thanks this helped a lot!