JavaScript onKeyUp 时间延迟
我需要一些有关名为 onKeyUp 的 JavaScript 函数的帮助,它是一个 Ajax 函数,但每次我写入任何字符时,它都会调用该函数,这会降低页面性能,并且每次都会进行检查,这是用户对数据库的检查。
我尝试了这个:
var timer;
function chk_me(){
clearTimeout(timer);
timer=setTimeout(function validate(){...},1000);
}
但是我的验证函数有一个参数,所以我无法传递它,我这样调用该函数:
<input type="text" name="usuario" id="usuario" class="required" onKeyUp="chk_me();" minlegth="4" value=/>
是吗?我应该怎么办?
这是我的第一个问题,希望大家能理解,
谢谢 阿尔贝托
谢谢大家,因为我是新人,所以我无法用不同的答案来做到这一点,所以我用解决方案编辑了我的问题。 我发现这个: http:// /bytes.com/topic/javascript/answers/451142-need-advice-acting-only-last-onkeyup-event-series 并使用最后一个答案方法并添加一些代码,我给你留下代码,就像它说的那样,这不是最好的方法,但可以工作,所以如果你有其他方法,我会感激它并在这里使用它:
var keyCount = 0;
var timeoutCount = 0;
function handleKeyUp() // keyup handler
{
keyCount++;
setTimeout("compareCounts()", 500);
}
function compareCounts()
{
var usuario = document.profileForm.usuario.value;
timeoutCount++;
if (keyCount == timeoutCount)
{
xajax_checaUsuario(usuario);
}
}
所以我以这种方式使用 var 有什么建议可以使它更好吗?
谢谢
I need some help with a JavaScript function that I call onKeyUp, it is a Ajax function but every time I write any character it calls the function and it slow the page performance and it check every time, it is an user check with the database.
I tried this:
var timer;
function chk_me(){
clearTimeout(timer);
timer=setTimeout(function validate(){...},1000);
}
But my validate function have a parameter so I am unable to pass it, I call the function like this:
<input type="text" name="usuario" id="usuario" class="required" onKeyUp="chk_me();" minlegth="4" value=/>
Is it right? What should I do?
It is my first question and I hope that you all understand it
Thanks,
Alberto
Thank you all, since I am new I cant do it in a different answer so I edited my question with the solution.
I find this: http://bytes.com/topic/javascript/answers/451142-need-advice-acting-only-last-onkeyup-event-series and use the last answer method and add it some code, I leave you the code, like it say it isnt the best way but works so if you have some other way I will appreciate it and use it here it is:
var keyCount = 0;
var timeoutCount = 0;
function handleKeyUp() // keyup handler
{
keyCount++;
setTimeout("compareCounts()", 500);
}
function compareCounts()
{
var usuario = document.profileForm.usuario.value;
timeoutCount++;
if (keyCount == timeoutCount)
{
xajax_checaUsuario(usuario);
}
}
So I take the var in that way any suggestions to make it better?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要将
validate
直接传递给setTimeout
,而是从匿名函数中调用它:Don't pass
validate
directly tosetTimeout
, but rather call it from within an anonymous function:如果我正确理解你的问题,我认为你想要的是将你的参数包含在一个闭包中:
和以前一样,计时器确保取消任何先前的调用,除非有 1 秒的等待,并且当你在范围内定义 validate 时chk_me,局部变量 myparam 在其中可见,即使它的句柄传递给 setTimeout 也是如此。
If I understood your question correctly, I think what you want is to contain your parameter within a closure:
As before, the timer ensures any previous calls are canceled, unless there's been a 1 second wait, and when you define validate within the scope of chk_me, the local variable myparam will be visible inside it, even when it's handle is passed to setTimeout.
您可以执行以下操作
在 javascript 中,您可以访问定义函数的范围内的任何变量。下面是如何使用它。
You can do the following
In javascript you can access any variables in the scope in which the function is defined. And below is how you use it.