Javascript Vuejs - 在输入时,运行一个函数(但有延迟)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42511311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vuejs - On input, run a function (but with a delay)
提问by MonkeyOnARock
I have an input field, and v-on:inputit runs a method called activatethat looks like this:
我有一个输入字段,v-on:input它运行一个名为的方法activate,如下所示:
export default: {
data() {
return {
isHidden: true
}
},
methods: {
activate() {
this.isHidden = false;
}
}
}
isHiddenturns 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 inputit immediately turns on the activatefunction. Is there a way to, perhaps, put it on a delay via setTimeout? I've tried doing the following but it doesn't work:
因此,目前,当用户执行操作时,input它会立即打开该activate功能。有没有办法,也许,把它延迟通过setTimeout?我尝试执行以下操作,但不起作用:
methods: {
setTimeout(function() {
activate() {
this.isHidden = false;
}
}, 500)
}
回答by Joe Attardi
Try this:
尝试这个:
methods: {
activate() {
setTimeout(() => this.isHidden = false, 500);
}
}
回答by leshkin
Or without arrow function:
或者没有箭头功能:
methods: {
activate() {
var that = this;
setTimeout(function() { that.isHidden = false; }, 500);
}
}

