zoukankan      html  css  js  c++  java
  • JS判断用户连续输入

    方案1

    //
    // $('#element').donetyping(callback[, timeout=1000])
    // Fires callback when a user has finished typing. This is determined by the time elapsed
    // since the last keystroke and timeout parameter or the blur event--whichever comes first.
    //   @callback: function to be called when even triggers
    //   @timeout:  (default=1000) timeout, in ms, to to wait before triggering event if not
    //              caused by blur.
    // Requires jQuery 1.7+
    //
    ;(function($){
        $.fn.extend({
            donetyping: function(callback,timeout){
                timeout = timeout || 1e3; // 1 second default timeout
                var timeoutReference,
                    doneTyping = function(el){
                        if (!timeoutReference) return;
                        timeoutReference = null;
                        callback.call(el);
                    };
                return this.each(function(i,el){
                    var $el = $(el);
                    // Chrome Fix (Use keyup over keypress to detect backspace)
                    // thank you @palerdot
                    $el.is(':input') && $el.on('keyup keypress',function(e){
                        // This catches the backspace button in chrome, but also prevents
                        // the event from triggering too premptively. Without this line,
                        // using tab/shift+tab will make the focused element fire the callback.
                        if (e.type=='keyup' && e.keyCode!=8) return;
                        
                        // Check if timeout has been set. If it has, "reset" the clock and
                        // start over again.
                        if (timeoutReference) clearTimeout(timeoutReference);
                        timeoutReference = setTimeout(function(){
                            // if we made it here, our timeout has elapsed. Fire the
                            // callback
                            doneTyping(el);
                        }, timeout);
                    }).on('blur',function(){
                        // If we can, fire the event since we're leaving the field
                        doneTyping(el);
                    });
                });
            }
        });
    })(jQuery);
    
    $('#example').donetyping(function(){
      $('#example-output').text('Event last fired @ ' + (new Date().toUTCString()));
    });<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <input type="text" id="example" />
    <p id="example-output">Nothing yet</p>


    方案2.

    https://github.com/kenshin54/jquery-koala

    Load jQuery and koala:

    <script src="jquery.min.js" type="text/javascript"></script>
    <script src="jquery.koala.js" type="text/javascript"></script>

    Bind keyboard events to the dom with Koala.

    <script type="text/javascript">
       jQuery(document).ready(function() {
        $('.koala').koala({
          delay: 200,
          keyup: function(event){
            // do anything you want
            // ex. ajax
          }
        });
       });
    </script>

    Koala support descendant elements that are added to the document at a later time.

    <script type="text/javascript">
       jQuery(document).ready(function() {
        $("#future").koala('.koala', {
          delay: 300,
          keyup: function(event){
            // do anything you want
            // ex. ajax
          }
        });
       });
    </script>
  • 相关阅读:
    ORACLE PL/SQL编程总结(二)
    ORACLE PL/SQL基础编程
    Linux centos7环境下安装Nginx
    namespace 实例命名空间 及 应用命名空间 问题
    python 2.7 的django项目
    django项目 导出 和 安装 依赖包
    windows 2012 安装apache
    FX-玩列表
    记录pycharm快捷键出错的其中一个原因
    Django 安装配置
  • 原文地址:https://www.cnblogs.com/mschen/p/6182017.html
Copyright © 2011-2022 走看看