[英]delay the on(“click”) event
I have this code 我有这个代码
$("input").on('keyup', function () {
$("block"),slideDown("slow")......
The problem is taht when I write fast the block will do the "animation" again and agin much slower than I write 问题是,当我写得快时,该块将再次执行“动画”,并且比我写的要慢得多
Is there another event I can use to only run the code "after I'm finished writing" lets say, I stop writing and then it taks 500ms and then the code is executed. 还有另一个事件我可以用来仅在“完成编写后”运行代码时说,我停止编写,然后等待500毫秒,然后执行代码。
function throttle(fn, time ){
var timer = 0;
return function() {
clearTimeout(timer);
timer = setTimeout($.proxy(fn, this), time);
};
}
$("input").on('keyup', throttle(function () {
$("block").slideDown("slow")
},500));
The throttle returns a new function that calls the old function once 500 milliseconds have elapsed since the function was last called. 自上次调用该函数以来,一旦经过500毫秒,则油门将返回一个新函数,该函数将调用旧函数。
Something like this is what I've used before: 我以前使用过这样的东西:
$(input).keyup(onKeyUp);
/**
* Handler for the keyup event
*/
function onKeyUp() {
//reset
clearTimeout(myTimer);
myTimer = setTimeout(otherFunction, 500);
}
Now otherFunction will be called after 500 milliseconds, but on each keyup event this timer will be reset. 现在,将在500毫秒后调用otherFunction,但是在每次键入事件时,将重置此计时器。
Try to use... 尝试使用...
function slideDown(){
...
}
// call
setTimeout(function(){ slideDown(); }, 1000);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.