0

Clearing the setInterval from console I tried using

clearInterval(obj.timer())

this seems not be doing the trick any idea peeps

var obj = {
  timer: function timer() {
       setInterval( () => { console.log('welcome') },1000)
  }
}

obj.timer()

// how do I clearInteval from the console
Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65
webmansa
  • 85
  • 1
  • 12
  • Possible duplicate of [pause and resume setInterval in javascript](https://stackoverflow.com/questions/38395724/pause-and-resume-setinterval-in-javascript) – cнŝdk Dec 12 '18 at 08:24

2 Answers2

1

you need to store id returned by the setInterval function.

 var obj = {
    id: null,

    timer: function timer() {

        this.id = setInterval(() => {
            console.log('welcome');
        }, 1000)
    },
    stop: function stop() {
        clearInterval(this.id);
    }
}
obj.timer();

you can stop the timer by calling stop function obj.stop()

Pranay Kumar
  • 2,139
  • 1
  • 15
  • 15
0

Did you try this, add "return" into function

var obj = {
            timer: function timer() {
                 return setInterval( () => { console.log('welcome') },1000)
            }
           };
var timer = obj.timer();
//clear timer
clearInterval(timer);
kntrieu
  • 64
  • 3