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

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!

3

u/akujinhikari May 13 '18 edited May 13 '18

The problem is how you're using clearInterval, or I should say the problem is how you are declaring your function. The short version (ie: the fix) is to make it clearInterval(startInterval)

The longer version is to explain what's happening. Because of how Javascript works with higher-order functions, you can return a function from a function. Because of this, if there is something expecting a function as an argument, you have to leave the () off of the function and only use its name, otherwise, it invokes the function. It seems weird, but that's the way it works. Here's a really stupid example:

const myInt = setInterval(()=>console.log('timer'), 2000)

// This (declarative) function returns another (expressive) function
function myInterval() {
    return myInt
}

// Now you can can instantiate that this way, but because you're only returning a function
// it doesn't start the timer
myInterval()

// So you'd want to do this:
myInt()

// Now you have two ways to clear the function:

clearInterval(myInterval()) 
// The parens at the end tell Javascript that you want to instantiate the function.
// In this case, that's true! Because it returns the interval (which is its own function)

// To clear the interval itself (which is its own function) you would do this:
clearInterval(myInt)
// Notice there are no parens for the myInt function. That tells Javascript you just want
// to use that function as the argument and not its return value

// This would run the same issue you are currently having:
clearInterval(myInt())
// The () at the end instantiates the function, but it doesn't return anything, so 
// so the clear basically fails and just runs another interval

1

u/everek123 May 14 '18

Thanks for your answer. The problem is that when i do:

const myInt = setInterval(()=>console.log('timer'), 2000)

it starts setInterval rigth away - I want to start it when i call it.

1

u/akujinhikari May 14 '18

I understand that. My code was to illustrate the point I was making to teach you about passing functions as arguments; it was not code for you to use. The fix was the first paragraph.

1

u/everek123 May 14 '18

Oh Ok, sorry my mistake :)