0
我想和事件發射器在輸入時有一點點延遲(冷卻時間),例如1秒輸入輸入,所以輸入一個字會觸發多次,但只有1第二個最後的擊鍵瞭解自定義JQuery事件以及如何修改它slrthly
後,我發現這個代碼
$.fn.changeOrDelayedKey = function(fn, iKeyDelay, sKeyEvent) {
var iTimeoutId,
oEventData;
// second signature used, update the variables
if (!$.isFunction(fn)) {
oEventData = arguments[0];
fn = arguments[1];
iKeyDelay = arguments[2];
sKeyEvent = arguments[3];
}
if (!iKeyDelay || 0 > iKeyDelay) {
iKeyDelay = 500;
}
if (!sKeyEvent || !this[sKeyEvent]) {
sKeyEvent = 'keydown';
}
// non-delayed event callback, should clear any timeouts, then
// call the original callback function
function fnExecCallback() {
clearTimeout(iTimeoutId);
fn.apply(this, arguments);
}
// delayed event callback, should call the non-delayed callback
// after a short interval
function fnDelayCallback() {
var that = this,
args = arguments;
clearTimeout(iTimeoutId);
iTimeoutId = setTimeout(function() {
fnExecCallback.apply(that, args);
}, iKeyDelay);
}
if (oEventData) {
this.change(oEventData, fnExecCallback);
this[sKeyEvent](oEventData, fnDelayCallback);
}
else {
this.change(fnExecCallback);
this[sKeyEvent](fnDelayCallback);
}
return this;
};
哪一種做到這一點,但它也會觸發change
,這意味着,如果我寫的東西不動,後移動焦點說,5秒,然後當我終於,事件再次發生,它不應該。所以我只希望事件發生在keyup
事件中。
如果任何人都可以解釋如何使用該方法的工作,我怎麼可能修改,以適應我的用例,這將是非常讚賞
作品像我想的更簡單,感謝您 – Tarlen 2015-02-08 10:05:09
太客氣 – DelightedD0D 2015-02-08 10:20:16