我正在尝试创建类似'onFinishTyping'的东西,它将超时设置为3s,如果用户在这3秒内写了一些东西,我需要销毁那个计时器并设置新的..问题是在每个按钮点击事件被触发之后。
这是我所拥有的:
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 3000; //time in ms
// Notice: jq = jQuery.noConflict();
jq(document).ready(function(){
//on keyup, start the countdown
jq('.table-search-field').keyup(function(event){
var typingTimer = setTimeout(function(){
doneTyping(event.target);
}, doneTypingInterval);
});
//on keydown, clear the countdown
jq('.table-search-field').keydown(function(){
clearTimeout(typingTimer);
});
});
//user is "finished typing," do something
function doneTyping (field) {
var value = jq(field).val().toLowerCase();// lower case
jq.ajax('index.php?option=com_mycomponent&task=player.search&keyword='+value)
.done(function(data){
console.log('ok');
}).fail(function(){
console.log('fail');
});
};