A
A
Alexander Ivanov2018-03-26 17:40:36
Vue.js
Alexander Ivanov, 2018-03-26 17:40:36

How to inherit value from methods in Vue?

<div @mouseenter="mouseEnter"  @mouseleave="mouseLeave">
                <h1>Hover me!</h1>
            </div>

methods: {
            mouseEnter() {
                setInterval(function() {
                    console.log( "тик" );
                }, 500);
            },
            
            mouseLeave() {
                setTimeout(function() {
                    this.mouseEnter()
                    console.log( "достаточно" );
                }, 1);
            }
      }

Above trying to stop the execution of the function the
console complains about this.mouseEnter() - not a function

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-03-26
@cimonlebedev

trying to stop the function

I don't see anything like that at all. To cancel the setInterval action, you need to call the clearInterval method, and of course, first save the timer id:
data: () => ({
  intervalID: null,
}),
methods: {
  mouseEnter() {
    this.intervalID = setInterval(() => {
      console.log('тик');
    }, 500);
  },
  mouseLeave() {
    clearInterval(this.intervalID);
    this.intervalID = null;
  },
},

E
Evgeny Kulakov, 2018-03-26
@kulakoff Vue.js

mouseLeave() {
                setTimeout(() => {
                    this.mouseEnter()
                    console.log( "достаточно" );
                }, 1);
            }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question