1

I am using this example with jQuery 1.9.1

How to delay the .keyup() handler until the user stops typing?

to delay the keyup request after user stop typing.

    // Custom Delay Function
    var delay = (function(){
        var timer = 0;

        return function(callback, ms){
            clearTimeout (timer);
            timer = setTimeout(callback, ms);
          };
    })();

    // Match Old Password
    $('input[name="old_password"]').keyup(function(){
        delay(function(){
            var data = $.trim($(this).val());
            // Send request to check
            /*$.post('admin/ajax/passReq.php', {action: 'old_match', data: data}, function(response){
                console.log('working');
            });*/
            console.log('working');
          }, 2000 );
    });

but i am getting the typeError: o.nodeName is undefined in jquery :(

is this not working on 1.9.1 or i have to use this with another way?

UPDATE: http://jsfiddle.net/jogesh_pi/6mnRj/1/

4

3 に答える 3

1

this遅延呼び出しの中で使用します。テキスト$(this)ボックスにはなりません。

遅延関数呼び出しの外に移動します。

$('input[name="old_password"]').keyup(function(){
    var el = $(this);
    // ^^^^^^^^^^^^^
    delay(function(){
      ...
      }, 2000 );
});
于 2013-07-26T05:11:24.993 に答える
0
// Match Old Password
$('input[name="old_password"]').keyup(function(){
    var el = $(this); //you need this this.
    delay(function(){
        var data = $.trim(el.val());
        // Send request to check
        /*$.post('admin/ajax/passReq.php', {action: 'old_match', data: data}, function(response){
            console.log('working');
        });*/
        console.log('working');
      }, 2000 );
});
于 2013-07-26T05:08:44.663 に答える
0

変化する

var data = $.trim($(this).val());

var data = $.trim($('input[name="old_password"]').val());

あなたのコードは実際にはほとんど正しいです。

于 2013-07-26T05:13:19.107 に答える