我正在使用以下keyUpDelay
函数将keyup
上的按键之间的代码执行延迟1秒。如何改变这一点,以便如果按下回车键,计时器将立即完成并执行代码。
var keyUpDelay = (function () {
var timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
$('input').keyup(function () { // AUTO SEARCH FUNCTION
keyUpDelay(function () {
// code to delay by 1000 ms except if enter key is pressed
}, 1000);
});
答案 0 :(得分:0)
这个怎么样
keyUpDelay = (function () {
var timer = keyUpDelay.timer = 0;
return function (callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
}; })();
$('input').keyup(function (e) { // AUTO SEARCH FUNCTION
if (e.which==13) {
clearTimeout (keyUpDelay.timer);
cb();
return;
}
keyUpDelay(cb , 1000);
function cb () {
// code to delay by 1000 ms except if enter key is pressed
}
});