输入输入时的用户空闲时间 [英] User idle time while typing in input

查看:67
本文介绍了输入输入时的用户空闲时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是搜索功能,该功能应在用户停止在我的$("input#q)字段中键入至少100ms之后调用我的doSearch()方法.

The problem I'm having is a search function which should call my doSearch()-method after the user stopped typing for at least 100ms in my $("input#q) field.

我试图通过使用 answer 的逻辑来实现这一目标,但我对应该设置的位置感到困惑/取消设置setInterval(),使idleTime递增.

I tried to achieve this by using the logic of this answer, but I'm stuck with where I should set/unset the setInterval() which increments the idleTime.

var idleTime = 0;

$("input#q").keyup(function() {
  idleTime = 0;
  idleInterval = setInterval(function() {
    idleTimeIncrement();
  }, 25);
});

function idleTimeIncrement() {
  idleTime += 25;
  if (idleTime >= 100) {
    doSearch($("input#q").val());
  }
}

我在Firebug控制台中遇到的错误是:

The error I'm getting in Firebug Console says:

递归过多.

Too much recursion.

所以我想我的代码中有一个无限循环...;-)

So I guess I'm having an infinite loop in my code... ;-)

推荐答案

启动新计时器时,您需要重置计时器.这就是我要做的:

You need to reset your timer when you start a new one. Here's what I would do:

var timer = null;
$('input#q').keyup(function() {
    if (timer) {
        clearTimeout(timer);
    }
    timer = setTimeout(function() {
        doSearch($('input#q').val())
    }, 100);
});

这篇关于输入输入时的用户空闲时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆