3

我想在我的网页中添加以下功能:如果用户在 30 秒内没有按下“接受”按钮,则会$('#popupBoxAccept').click( 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>
4

5 回答 5

7

jsFiddle 演示

var tim = setTimeout(function(){
    $('#popupBoxAccept').click();
},30000);

手动点击里面:

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

正如@Elias 所建议的那样:

$('#popupBoxDecline').click( function() {
    clearTimeout(tim);           
    //...
});
于 2012-09-06T09:33:57.020 回答
4

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

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

setTimeout(function() {
    if (!clicked) {
        $("#popupBoxAccept").click();
    }
}, 30000);
于 2012-09-06T09:34:07.667 回答
1

试试这个

setTimeOut(function(){
  $('#popupBoxAccept').click( function() {            
    //...
});
}, 30000);
于 2012-09-06T09:34:18.467 回答
1

只需设置一个超时:

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);
});

现在,不使用EVIL全局变量:

$(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 tim在另一个上下文中使用,而不会丢失对超时的引用。闭包,名字就说明了一切:所有变量和引用都整齐地捆绑在同一个范围内,并且不能访问,只能在声明的范围内访问:

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
于 2012-09-06T09:37:23.710 回答
0

尝试将其放入您的$(document).ready()通话中:

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

然后改变

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

var clicked = false;
$('#popupBoxAccept').click( function() {
    clicked = true;
    //...
});
于 2012-09-06T09:34:36.253 回答