问题描述
我有一个简单的过去事件
I have a simple past event as
document.getElementById('paste_area').addEventListener('paste', function() {
document.getElementById('notice').innerHTML='Text was successfully pasted!';
alert('Pasted');
}, true);
可以在此处找到工作示例
A working example can be found here http://jsfiddle.net/XEQzz/
警报和通知将在粘贴之前出现。 我该如何延迟粘贴事件实际完成之后的警报操作?
The alert and notice will appear before pasting. How can I delay the alert action to happen after paste event has been actually completed?
推荐答案
您可以将您的警报放入 setTimeout
。
You could put your alert in a setTimeout
.
setTimeout(function() {alert('Pasted');}, 0);
这会将代码延迟到值更新后。
This will delay the code until after the value has updated.
请记住, setTimeout
回调中的 this
的值将与
Just keep in mind that this
in the setTimeout
callback will have a different value than that in the enclosing environment.
如果您需要引用外部 this
,它将作为元素,然后在变量中引用它。
If you'll need a reference to the outer this
, which will be the element, then reference it in a variable...
var self = this;
setTimeout(function() {alert(self.value);}, 0);
或使用 .bind()
(大多数支持 addEventListener
的浏览器都支持。较早的Safari不支持。) ...
Or use .bind()
(Supported in most browsers that support addEventListener
. Older Safari didn't support it.)...
setTimeout(function() {alert(this.value);}.bind(this), 0);
这篇关于如何在Javascript中粘贴事件后发出警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!