Jquery 延迟事件直到用户完成输入
比方说,在用户完成最后一个键入后一秒半启动的功能。我有这段代码,但每次用户创建一种类型时,都会启动我不想要的脚本:
$(document).ready(function()
{
var $firstname = $("#firstname");
$firstname.keyup(function()
{
var content = $("#firstname").val();
$.post("ajax.php", { firstname: content}, function(result){
var contentDiv = $("#ContainerValidation");
contentDiv.fadeOut(400, function(){ contentDiv.html(result); });
contentDiv.fadeIn(400);
contentDiv.fadeOut(5000);
});
});
});
感谢您的帮助。
A function to be launched let's say a second and a half after the user has finished typing the last type. I have this piece of code but everytime that the user is making one type the script is launched which I don't want:
$(document).ready(function()
{
var $firstname = $("#firstname");
$firstname.keyup(function()
{
var content = $("#firstname").val();
$.post("ajax.php", { firstname: content}, function(result){
var contentDiv = $("#ContainerValidation");
contentDiv.fadeOut(400, function(){ contentDiv.html(result); });
contentDiv.fadeIn(400);
contentDiv.fadeOut(5000);
});
});
});
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为启动一个等待 1.5 秒的计时器就足够了,如果用户在这段时间内继续输入,计时器就会中止:
I think it would be sufficient to start a timer that waits 1.5 seconds and is aborted if the user continues typing during that time:
您需要的是一个去抖动脚本。
一个非常简单的解决方案是嵌入 Underscore.js 库,它包含这样一个函数: http://documentcloud.github.com/underscore/#debounce
你可以像这样使用它:
What you'll want is a debouncing script.
A very easy solution would be embedding the Underscore.js library, it contains such a function: http://documentcloud.github.com/underscore/#debounce
You'd use it like this: