在一个人停止输入后如何调用函数? [英] How can I call a function after a person stops typing?

查看:76
本文介绍了在一个人停止输入后如何调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在HTML页面上有一个输入,其中一个人会输入内容,而我想要的是每次停止输入时都要调用的功能。我知道onkeypress事件,但每次输入一个字符时都会调用它。这是我想要的一个例子:

I have an input on an HTML page where a person will type something and what I'm wanting is for a function to be called every time they stop typing. I know about the onkeypress event, but that is called every time a character is typed. Here's an example of what I would like:


  1. 人物类型这是对输入的测试

  2. 此人停止输入后,应调用函数foo()

  3. 人物在输入中输入其他内容

  4. 此人停止输入后,应该调用函数foo()

  5. 重复....

  1. Person types "this is a test" into the input
  2. After the person stops typing, function foo() should be called
  3. Person types something else into the input
  4. After the person stops typing, function foo() should be called
  5. Repeat....

这是可能的吗? ?

非常感谢!

推荐答案

收听onkeyupevent。开始超时(您必须将停止定义为某个时间量,否则将在每个键盘处调用),并在超时时执行您的功能。

Listen to onkeyupevent. There start a timeout (you have to define "stop" as some time amount, otherwise it will be called at every keyup), and in the timeout execute your function.

如果在计时器运行时调用onkeydown,请将其停止。

If onkeydown is called while your timer is running, stop it.

类似于......

var timer;

function onKeyUpHandler(e){
    timer = setTimeout("foo()", 500)    
}

function onKeyDownHandler(e) {
    clearTimeout(timer);
}

此代码的确切外观取决于您在何处以及如何使用它...只是为了说明这样做的方法。

How this code exactly looks depends where and how you are using it... just to show the way to do it.

这篇关于在一个人停止输入后如何调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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