事件被触发 n 秒之内只会执行一次事件处理函数

    1. function throttle (fn, delay) {
    2. var t = null,
    3. begin = new Date().getTime();
    4. return function () {
    5. var _self = this,
    6. args = arguments;
    7. var cur = new Date().getTime();
    8. clearTimeout(t);
    9. if (cur - begin >= delay) {
    10. fn.apply(_self, args);
    11. begin = cur;
    12. } else {
    13. t = setTimeout(function () {
    14. fn.apply(_self, args);
    15. }, delay);
    16. }
    17. }
    18. }