JS按键延迟处理

原文请点击这里
页面触发按键后,在设定时长内重复按键将不进行处理,超出设定时长则触发JS业务,代码如下

$().ready(function(){
    var _timer = {};
    var converter = new showdown.Converter()
    var delay_till_last = function(id, fn, wait) {
        if (_timer[id]) {
            window.clearTimeout(_timer[id]);
            delete _timer[id];
        }

        return _timer[id] = window.setTimeout(function() {
            fn();
            delete _timer[id];
        }, wait);
    }

    var submit_form = function(e) {
        $('#wmd-preview').html(converter.makeHtml($('#wmd-input').val()));
        return false;
    };
    $('#wmd-input').bind('keyup', function(e) {
        delay_till_last('check_input', submit_form, 800);        
    });
  });

你可能感兴趣的:(JS按键延迟处理)