首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript cleartimeout()问题

javascript cleartimeout()问题
EN

Stack Overflow用户
提问于 2013-03-16 02:11:15
回答 4查看 2.1K关注 0票数 1

我的主要问题是: clearTimeout()是否停止计时器并且不执行计时器中的函数?或者只是清除超时并立即执行函数?

下面是我的例子

代码语言:javascript
复制
$('input').keyup(function(){
        var thisClass = $(this).attr('class').split(" ");
        var thisValue = $(this).val();
        var thisError = $(this).parent().siblings('.error');
        var thisTimeout;
        if(thisClass[1] == "letters"){
            if(thisValue.length <= 1){
                thisTimeout = setTimeout(function(){
                    thisError.html("You must have at least 2 characters");  
                    thisError.show();
                }, 800);
            } else {
                clearTimeout(thisTimeout);
                thisError.hide();
                thisError.html(""); 
            }
        }

    });

这是代码检查的一部分,检查输入框中是否至少有2个字符。目前,如果此代码包含2个或更少的字符,则它在800毫秒之后很好地执行超时。当输入框有2个以上的字符时,如果我的键入速度足够快,当我完成键入时,错误框仍然会出现。我可以通过多键入一个字符来清除错误,但是如果我快速键入我的名字,当我完成键入时,错误就会显示出来。

我还有这段代码:

代码语言:javascript
复制
$('input').keydown(function(){
        clearTimeout(thisTimeout);
        thisError.hide();
    });

希望如果我继续输入想法,它会清除错误吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-03-16 02:21:21

在你的第一个问题中,你问了:

clearTimeout()是否停止计时器,并且不在计时器内执行函数?或者只是清除超时并立即执行函数?

答案是第一个选项- clearTimeout停止计时器并且不执行计时器回调。

代码问题中的问题是因为thisTimeout是在keyup函数中声明的。keydown函数没有对thisTimeout的引用。

您应该将其移动到共享作用域中:

代码语言:javascript
复制
var thisTimeout;

$('input').keyup(function(){
    var thisClass = $(this).attr('class').split(" ");
    var thisValue = $(this).val();
    var thisError = $(this).parent().siblings('.error');
    if(thisClass[1] == "letters"){
        if(thisValue.length <= 1){
            thisTimeout = setTimeout(function(){
                thisError.html("You must have at least 2 characters");  
                thisError.show();
            }, 800);
        } else {
            clearTimeout(thisTimeout);
            thisError.hide();
            thisError.html(""); 
        }
    }
});

$('input').keydown(function(){
        clearTimeout(thisTimeout);
        thisError.hide();
    });
});
票数 1
EN

Stack Overflow用户

发布于 2013-03-16 02:14:58

您可能正在覆盖以前的setTimeout id。因此,您将无法清除它。

代码语言:javascript
复制
var thisTimeout;
$('input').keyup(function(){
    var thisClass = $(this).attr('class').split(" ");
    var thisValue = $(this).val();
    var thisError = $(this).parent().siblings('.error');

    if(thisClass[1] == "letters"){
        if(thisValue.length <= 1){
            clearTimeout(thisTimeout); // <-- already clear the timeout here before 
                                       // starting another one
            thisTimeout = setTimeout(function(){
                thisError.html("You must have at least 2 characters");  
                thisError.show();
            }, 800);
        } else {
            clearTimeout(thisTimeout);
            thisError.hide();
            thisError.html(""); 
        }
    }

});
票数 4
EN

Stack Overflow用户

发布于 2013-03-16 02:21:18

您必须将thisTimeout移动到更高的作用域,以便保留它的值,并且在覆盖之前的值之前必须清除它:

代码语言:javascript
复制
var thisTimeout;
$('input').keyup(function(){
    var thisClass = $(this).attr('class').split(" ");
    var thisValue = $(this).val();
    var thisError = $(this).parent().siblings('.error');
    if(thisClass[1] == "letters"){
        if(thisValue.length <= 1){
            clearTimeout(thisTimeout);
            thisTimeout = setTimeout(function(){
                thisError.html("You must have at least 2 characters");  
                thisError.show();
            }, 800);
        } else {
            clearTimeout(thisTimeout);
            thisError.hide();
            thisError.html(""); 
        }
    }

});

仅供参考,这段代码也可以清理一下:

代码语言:javascript
复制
var thisTimeout;
$('input').keyup(function(){
    var self = $(this);
    var thisError = self.parent().siblings('.error');
    if (self.hasClass("letters")) {
        if(self.val().length <= 1){
            clearTimeout(thisTimeout);
            thisTimeout = setTimeout(function(){
                thisError.html("You must have at least 2 characters");  
                thisError.show();
            }, 800);
        } else {
            clearTimeout(thisTimeout);
            thisError.hide();
            thisError.html(""); 
        }
    }

});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15439368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档