你正在做正确的方式家伙。
var isProcessingEvent = false;
$("#textarea").on('click keypress cut paste', processUserInput);
function processUserInput(e) {
// Is processing event, so stop here.
if(isProcessingEvent) {
return;
}
isProcessingEvent = true;
// do the actual processing of user input
isProcessingEvent = false;
}
但是,如果我想你,我会用一个promisses
与用户输入的处理工作,这样你:只要你改变为keypress
,但你可以得到你的代码的时尚,如果你想更好的无法在处理过程中冻结所有UI线程。
会是这样的:
$("#textarea").on('click keypress cut paste', processUserInput);
function processUserInput(e) {
// Is processing event, so stop here.
if(processUserInput.working) {
// The user trigger the event while it was processing
processUserInput.doAgain = {
// save context
ctx: this,
// save event
e: e
};
return;
}
processUserInput.working = true;
function finished() {
processUserInput.working = false;
// The process finished but has new changes in the textfield so...
var lastEvent = processUserInput.doAgain;
if (lastEvent) {
processUserInput.doAgain = null;
// Process this guy again
setTimeout(processUserInput.bind(lastEvent.ctx), 0, lastEvent.e);
}
}
function theProcess(e, cb) {
// do my async stuff here
// Unfreeze the click/keydown/cut/past events in the textarea
if (typeof cb === 'function') {
cb();
}
}
setTimeout(theProcess.bind(this), 0, e, finished);
}
这是异步一个例子,但你可以使用一个异步Ajax或web的工人来处理您的活动,这样你就不会冻结UI线程。
PS .:超时不会阻止UI线程冻结,它只会将您的进程放到执行队列的末尾。
Ahh另一个提示!
如果您正在处理textarea中的文本,那么最好使用keypress
而不是,因为如果您在keydown中获得textarea值,它将不会有更改,但按键会获得由您更改的值正在紧迫。
http://www.quirksmode.org/dom/events/keys.html
当然,如果你仍然想使用的keydown,你可以使用我的例子进行了setTimeout
推迟处理。
不确定,但尝试使用event.stopPropagation() – sdespont
@sdespont将无法正常工作,因为它们是不同的事件。它会停止keydown传播,然后执行过去并停止传播。 –