麻烦将setTimeout添加到我的jQuery脚本中

时间:2013-10-03 13:05:22

标签: javascript jquery popup settimeout

我在将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按钮时显示一次。

1 个答案:

答案 0 :(得分: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