Why clearInterval does not work with function

this is the code

var t = ()=>{

    setInterval(()=>{

        console.log('hello')

    },1000)


}

t();

clearInterval(t)

Why does clearinterval not block the execution of setInterval?

+4
source share
4 answers

He does not work on the function because the mechanism was developed right now. Calls setInterval()return a number that acts as the identifier of the timer that sets the call. This number must be passed in clearInterval().

This does not result in an error in transmitting something that is not a number, or in transmitting a number that does not identify the active timer, but the call has no effect.

t() setInterval(), , .

+4

, .

:

setInterval , , , clearInterval().

//function that keeps logging 'hello' on the console
var t = ()=>{
    //return the id of the interval
    return setInterval(()=>{
        console.log('hello')
    },1000)

}

//get the id of the interval created by the "t" function
var id = t();
//alerts the id just to see it
alert(id);
//clear it - function will stop executing
clearInterval(id);
Hide result

https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval

+4

Interval setInterval().

var interval = setInterval();
clearInterval(interval); 
+1

T setInterval, .

:

var t = ()=>
    setInterval(()=>{
        console.log('hello')
    },1000)

var interval = t();
clearInterval(interval);
0

Source: https://habr.com/ru/post/1681970/


All Articles