Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript setTimeout仅在导致它的事件的第一次发生时不起作用_Javascript_Jquery_Settimeout_Blur - Fatal编程技术网

Javascript setTimeout仅在导致它的事件的第一次发生时不起作用

Javascript setTimeout仅在导致它的事件的第一次发生时不起作用,javascript,jquery,settimeout,blur,Javascript,Jquery,Settimeout,Blur,在下面的代码中,我想设置var hasFocus=False。此代码立即变为hasFocus=False,而无需第一次等待500毫秒。之后,它会按预期工作。 我无法理解为什么它第一次没有像预期的那样运行 $(function(){ var hasFocus=false; $("textarea[name='question1'], input[name^=q1_option]").blur(function(event){ setTimeout(function

在下面的代码中,我想设置
var hasFocus=False
模糊500毫秒后编码>。此代码立即变为
hasFocus=False
,而无需第一次等待500毫秒。之后,它会按预期工作。
我无法理解为什么它第一次没有像预期的那样运行

$(function(){
    var hasFocus=false;

    $("textarea[name='question1'], input[name^=q1_option]").blur(function(event){
        setTimeout(function(){
            hasFocus=false;
        },500);
    }).focus(function(){
       hasFocus=true;
    });

    $(document).on('click',function(e){
        console.log("focus :" +hasFocus);
        if(hasFocus)
        {
           alert("Working!!"); //Now this does not come up for the first time!
        } 
    })
});

这是因为在触发超时模糊事件之前执行
console.log
,因此console显示以前的值。移动
console.log(“焦点:+hasFocus”)
blur
focus
功能中,查看所有功能是否正常工作

演示-

只需将这个:function(){hasFocus=true;}添加到setTimeout函数的末尾。或者创建一个函数,将hasFocus变量设置为true,并在setTimeout函数完成时调用此函数

您可以在此处阅读有关setTimeout函数的更多信息:


编辑:哦,如果这个方法不起作用,那么尼古拉是对的!:)

在经历了一番头重脚轻之后,我想到了这个。这不是我的确切代码,而是一个演示,它完全按照我想要的方式工作:


您确定这不是
var hasFocus=false就在顶部?你说的
代码立即变成hasFocus=False是什么意思?将变量设置为
var hasFocus=false在代码的第二行。你希望
hasFocus
有什么价值?@dotnetom你必须先聚焦它,然后才能模糊它@切夫,我可能错了,但我现在做的是先将其设置为false,然后在文本区域的焦点上设置为true。现在这是真的,当文本区域出现模糊时,它应该会变回假,但500毫秒后。@t请尝试将
.focus(function()
更改为
。单击(function())
这可能是因为浏览器不兼容。我完全同意你的答案,我也在代码中尝试了这一点,但在我的情况下不起作用。除此之外,我刚刚更新了代码以准确显示我正在做的事情。我的代码中没有其他内容出现这种情况。你还有什么可以建议的吗?谢谢你的时间!我刚刚添加了t他对您的问题进行了评论,并更新了小提琴。它在那里工作得很好,同意吗?是的,我想我必须多搔搔头了,因为代码不多,一定是有一些愚蠢的错误。它只是对我隐瞒了:/再次感谢!您如何注意到
代码立即变成hasFocus=False
?您在控制台中看到即时消息了吗?可以吗你可以为页面共享完整的html和javascript吗?不,不工作,你能给我看一个工作演示吗?
        setTimeout(function(){
        hasFocus=false;
    },500, function() {hasFocus=true;});
 $(function(){
    var hasFocus=false;

    $("textarea[name='question1'], input[name^=q1_option]").blur(function(event){
        setTimeout(function(){
            hasFocus=false;
        },500);
    }).focus(function(){
       setTimeout(function(){ //just initiated focus event after blur event
              hasFocus=true; //just initiated focus event after blur event
          },510); //just initiated focus event after blur event
    });

    $(document).on('click',function(e){
        console.log("focus :" +hasFocus);
        if(hasFocus)
        {
           alert("Working!!"); //Now this does not come up for the first time!
        } 
    })
});