2017-03-02 135 views

回答

2

你需要去抖动功能。请使用lodash _.debounce或编写自己的简单程序。然后使用它是这样的:

jQuery("#width").on('input', debounce(function() { 
    alert('hello') 
}, 1500)) 

简单的防抖动功​​能,可以是这个样子:

function debounce(callback, delay) { 
    var timeout 
    return function() { 
    var args = arguments 
    clearTimeout(timeout) 
    timeout = setTimeout(function() { 
     callback.apply(this, args) 
    }.bind(this), delay) 
    } 
} 

这里是一个演示:

jQuery("#width").on('input', debounce(function() { 
 
    alert('hello'); 
 
}, 1500)); 
 

 
function debounce(callback, delay) { 
 
    var timeout 
 
    return function() { 
 
    var args = arguments 
 
    clearTimeout(timeout) 
 
    timeout = setTimeout(function() { 
 
     callback.apply(this, args) 
 
    }.bind(this), delay) 
 
    } 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<input type="text" id="width" />

+0

伟人谢谢许多。 :) – MageLerner