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

You*_*per 3 html javascript jquery

我想在我的网页上添加以下功能:如果用户在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>
Run Code Online (Sandbox Code Playgroud)

Rok*_*jan 7

jsFiddle演示

var tim = setTimeout(function(){
    $('#popupBoxAccept').click();
},30000);
Run Code Online (Sandbox Code Playgroud)

手册内点击:

$('#popupBoxAccept').click(function(){
     clearTimeout(tim);
      // ........
});
Run Code Online (Sandbox Code Playgroud)

并且正如@Elias所建议的那样:

$('#popupBoxDecline').click( function() {
    clearTimeout(tim);           
    //...
});
Run Code Online (Sandbox Code Playgroud)