Stop function (clearInterval) inside the object does not work

0

I have this object, it works fine but I wanted to add a property to stop, this is not working:

JS:

var kissesAni = {
    init: function () {
        kissesAni.callAni();
    },
    callAni: function(){
        window.setInterval(function(){
            kissesAni.makeImgAni();
        }, 30);
    },
    stop: function() {
        window.clearInterval(kissesAni.callFontFlakes);
    },
    makeImgAni: function(){
        ...
    }
};
kissesAni.init();
    window.setTimeout(function() {
        console.log('heya');
        kissesAni.stop();
    }, 5000);

JSfiddle

That is, it does console.log('heya') but the animation does not stop.

    
asked by anonymous 02.03.2016 / 15:06

1 answer

2

You need to put the return of setInterval inside a variable, and then use clearInterval.

Try something like:

var kissesAni = {
		interval: null,
    init: function () {
        this.interval = kissesAni.callFlakes();
    },
    callFlakes: function(){
        return window.setInterval(function(){
            kissesAni.fontFlake();
        }, 30);
    },
    stop: function() {
        window.clearInterval(this.interval); // já tentei com this em vez de "kissesAni"
    },
    fontFlake: function(){
       
    }
};
    
02.03.2016 / 15:19