Javascript 如果未按下按钮,则在30秒内自动执行该功能

Javascript 如果未按下按钮,则在30秒内自动执行该功能,javascript,jquery,html,Javascript,Jquery,Html,我想在我的网页中添加以下功能:如果用户在30秒内没有按下“接受”按钮,则$('#popuboxaccept')。单击(function(){..}将自动执行 $(document).ready(function() { loadPopupBox(); }); function loadPopupBox() { // to load the Popupbox $('#popup_box').fadeIn("slow"); } $('#popupBoxAccept

我想在我的网页中添加以下功能:如果用户在30秒内没有按下“接受”按钮,则
$('#popuboxaccept')。单击(function(){..}
将自动执行

$(document).ready(function() {
    loadPopupBox();
});

function loadPopupBox() {   // to load the Popupbox 
    $('#popup_box').fadeIn("slow");     
}

$('#popupBoxAccept').click( function() {            
    //...
});

$('#popupBoxDecline').click( function() {           
    //...
});

<div id="popup_box">
    <a id="popupBoxAccept">Accept</a>
    <a id="popupBoxDecline">Decline</a>     
</div>
$(文档).ready(函数(){
loadPopuBox();
});
函数loadPopuBox(){//以加载PopuBox
$('弹出框').fadeIn(“慢”);
}
$('#popuboxaccept')。单击(函数(){
//...
});
$('popuboxDecend')。单击(函数(){
//...
});
接受
减少
然后在手册内单击:

正如@Elias所建议的:

$('#popupBoxDecline').click( function() {
    clearTimeout(tim);           
    //...
});

一个简单的选项是使用标志和
setTimeout
函数:

var clicked = false;
$("#popupBoxAccept").click(function() {
    clicked = true;
    //...
});

setTimeout(function() {
    if (!clicked) {
        $("#popupBoxAccept").click();
    }
}, 30000);
试试这个

setTimeOut(function(){
  $('#popupBoxAccept').click( function() {            
    //...
});
}, 30000);

尝试将其放入
$(文档)。ready()
调用:

setTimeout(function() {
    if(!clicked) {
        $('#popupBoxAccept').click();
    }
}, 30000);
然后改变

$('#popupBoxAccept').click( function() {
    //...
});

只需设置一个超时:

var tim;//global, I'll provide a (more complex) solution that doesn't need them, too
function loadPopupBox() {   // to load the Popupbox 
    $('#popup_box').fadeIn("slow");
    tim = setTimeout(function()
    {
        $('#popup_box').click();
    },30000);
}

$('#popupBoxAccept').click( function() {            
    clearTimeout(tim);
});
现在,不使用邪恶的全局变量:

$(document).ready(function()
{
    (function(popup,accept,decline)
    {
        popup.fadeIn("slow");
        var tim = setTimeout(function()
        {
            accept.click();//<-- access to reference is preserved
        },30000);
        accept.click(function()
        {
            clearTimeout(tim);
            //do accept stuff
        });
        decline.click(function()
        {
            clearTimeout(tim);//<-- need that here, too!
            //do decline stuff
        });
    })($('#popup_box'),$('#popupBoxAccept'),$('#popupBoxDecline'));
    //pass all 3 elements as argument --> less typing, and is more efficient:
    //the DOM is only searched once for each element
});

优雅而简单的解决方案。Thx。@YouKuper很抱歉我的错误,我的一个拼写错误setTimeout现在改为setTimeout!我的错:)@Roko:千万别忘了清除
\popuboxDecept
处理程序中的超时。@EliasVanOotegem是的,很好的建议。@EliasVanOotegem是正确的。如果您单击“拒绝”,计时器处理程序仍将启动。实际上我也有同样的问题:)这段代码只是在页面加载30秒后将一个点击处理程序绑定到接受按钮。。。它甚至没有触发点击事件。。。
var clicked = false;
$('#popupBoxAccept').click( function() {
    clicked = true;
    //...
});
var tim;//global, I'll provide a (more complex) solution that doesn't need them, too
function loadPopupBox() {   // to load the Popupbox 
    $('#popup_box').fadeIn("slow");
    tim = setTimeout(function()
    {
        $('#popup_box').click();
    },30000);
}

$('#popupBoxAccept').click( function() {            
    clearTimeout(tim);
});
$(document).ready(function()
{
    (function(popup,accept,decline)
    {
        popup.fadeIn("slow");
        var tim = setTimeout(function()
        {
            accept.click();//<-- access to reference is preserved
        },30000);
        accept.click(function()
        {
            clearTimeout(tim);
            //do accept stuff
        });
        decline.click(function()
        {
            clearTimeout(tim);//<-- need that here, too!
            //do decline stuff
        });
    })($('#popup_box'),$('#popupBoxAccept'),$('#popupBoxDecline'));
    //pass all 3 elements as argument --> less typing, and is more efficient:
    //the DOM is only searched once for each element
});
var foo = (function()
{
    var invisible = 'Now you see me';
    return function()
    {
        return invisible;
    }
};
console.log(invisible);//<-- undefined
var invisible = 'Now you don\'t';
console.log(foo());//<-- Now you see me
console.log(invisible);//Now you don\'t