开发者

execute javascript after focused on an element

I need to run execFunc(); not only when the user moves onto the ne开发者_如何学JAVAxt field, but also runs when the user remains focused on the same field for 5 seconds.

$('input[name="email"]').bind('blur', function () {
 execFunc();
});


var timer = null;
$('input[name="email"]').blur(function(e){
  if (timer) clearTimeout(timer);
  execFunc();
}).focus(function(e){
  timer = setTimeout(execFunc, 5000);
});

If you're doing form validation, there are better ways of doing this.


How about...

$('input[name="email"]').bind('blur', execFunc).bind('focus', function() {
    setTimeout(execFunc, 5000);
});

If the function isn't idempotent and you must execute it only once, you can do:

var execFuncHasExecuted = false;
function execFunc() {
    if (execFuncHasExecuted)
        return false;
    execFuncHasExecuted = true;

    // ... remainder of implementation
}


This should suffice

var g_timeout = null;

$('input[name="email"]')
      .blur(function () {
           execFunc();
      })
      .focus(function(){
           if (g_timeout) clearTimeout(g_timeout);
           g_timeout= setTimeout(execFunc, 5000);
      });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜