我發送一個jQuery $ .post請求在窗體中的任何複選框更改。 我想要的是將$ .post延遲500毫秒,以防用戶快速檢查多個複選框,以避免多次無用的請求。
這裏是我的代碼,我添加了似乎與此除外.post的$功能一切工作setTimeout函數...
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(document).ready(function() {
$('.checkbox').change(function() {
delay(function(){
$.post("/?page_id=4", $("#selectors").serialize(), function(data){
$('#results').html(data);
});
});
}, 1000);
});
知道爲什麼這不起作用?
我通常使用的setTimeout這樣的:'計時器= window.setTimeout(函數,1000);' – ipalaus 2011-03-04 14:30:50
是不是「},1000」在錯誤的地方?它不應該是delay()函數的第二個參數,而不是change()函數? – 2011-03-04 14:32:06
@ElianEbbing @Phil Elian說。你將'1000'傳遞給'.change'。另外默認'timer'爲'undefined',否則你會不小心使用'id === 0'終止'setTimeout'' – Raynos 2011-03-04 14:34:45