简体   繁体   English

Vuejs - 输入时,运行一个函数(但有一个延迟)

[英]Vuejs - On input, run a function (but with a delay)

I have an input field, and v-on:input it runs a method called activate that looks like this: 我有一个输入字段,并且v-on:input它运行一个名为activate的方法,如下所示:

export default: {
    data() {
        return {
            isHidden: true
        }
    },
    methods: {
        activate() {
            this.isHidden = false;
        }
    }
}

isHidden turns on/off some icon (it doesn't really matter what this data property is; I'm just using it for example purposes). isHidden打开/关闭一些图标(这个数据属性是什么并不重要;我只是将它用于示例目的)。

So currently, when a user does an input it immediately turns on the activate function. 所以目前,当用户进行input它会立即打开activate功能。 Is there a way to, perhaps, put it on a delay via setTimeout ? 有没有办法让它通过setTimeout延迟? I've tried doing the following but it doesn't work: 我尝试过以下操作,但它不起作用:

methods: {
    setTimeout(function() {
        activate() {
            this.isHidden = false;
        }
    }, 500)
}

Try this: 试试这个:

methods: {
  activate() {
    setTimeout(() => this.isHidden = false, 500);
  }
}

Or without arrow function: 或没有箭头功能:

methods: {
    activate() {
        var that = this;
        setTimeout(function() { that.isHidden = false; }, 500);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM