1

我在将 setTimeout 添加到我的 jQuery 代码时遇到了麻烦。

jQuery(document).ready( function() {
if (typeof(localStorage) == 'undefined' ) {
} else {
    var today = new Date();
    var lastmonth = new Date();
    lastmonth.setDate(today.getDate()-30);
    var popup = new Date(localStorage.getItem("popup"));
    if(popup<=lastmonth){showPopup();}
}
});


function showPopup() {
    jQuery('#popup').css({'width':'100%', 'height':'100%', 'visibility':'visible'});
}

function hidePopup() {
    var date = new Date();
    localStorage.setItem("popup",date);
    jQuery('#popup').css({'width':'0', 'height':'0', 'visibility':'hidden'});
}

此脚本加载一个弹出窗口,每月仅向查看者显示一次。我试图添加 setTimeout 但它总是会破坏脚本的工作。

你可以在这里看到它的工作链接;http://promotionalbusinessvideos.com/但是不要忘记它只会在您单击 X 按钮时显示一次。

4

1 回答 1

1

尝试

jQuery(document).ready( function() {
if (typeof(localStorage) !== 'undefined' ) { // fixed if clause

    var today = new Date();
    var lastmonth = new Date();
    lastmonth.setDate(today.getDate()-30);
    var popup = new Date(localStorage.getItem("popup"));

    if(popup<=lastmonth){

       setTimeout(function(){
          showPopup(); // wait 20 sec then called popup
       }, 2000); 

    }
}

function showPopup() {
    jQuery('#popup').css({'width':'100%', 'height':'100%', 'visibility':'visible'});
}

function hidePopup() {
    var date = new Date();
    localStorage.setItem("popup",date);
    jQuery('#popup').css({'width':'0', 'height':'0', 'visibility':'hidden'});
}

// assign show/hide popup to global scope (window)
window.hidePopup = hidePopup;
window.showPopup = showPopup;

}); // change the jquery scope
于 2013-10-03T13:15:37.117 回答