2013-11-01 31 views
1

我有一個通知欄,我只想在每個會話中顯示一次,或者可能沒有在預定時間內顯示通知。我正在使用會話存儲,但不幸的是我不能讓它正常工作。我如何才能讓通知欄在每個會話中僅顯示一次?

感謝

這裏是我的代碼----------->

<script> 
$(document).ready(function() { 

      var $alertdiv = $('<div id = "alertmsg"/>'); 
      $alertdiv.text("Join our email list and receive the latest updates at 3Elements Review."); 
      $alertdiv.bind('click', function() { 
       $(this).slideUp(200); 
      }); 
      $(document.body).append($alertdiv); 
      $("#alertmsg").fadeIn("slow"); 
      setTimeout(function() { $alertdiv.fadeOut(400) }, 6000); 
          }); 

$(window).load(function(){ 
     if(typeof(window.sessionStorage) != 'undefined') { 
     $("#alertmsg").val(window.sessionStorage.getItem('mySessionVal')); 
      window.sessionStorage.setItem('mySessionVal', $("#alertmsg").val()); 
      window.sessionStorage.setItem('storedWhen', (new Date()).getTime()); 
    } 
}); 
</script> 
+0

你爲什麼不使用cookie的? –

回答

3

有沒有明顯的理由使用的window.onload爲sessionStorage的,DOM準備好就足夠了?

此外,您還需要檢查,如果在顯示消息之前存儲的存在價值,否則該信息將始終顯示:

$(document).ready(function() { 
    if (typeof window.sessionStorage != undefined) { 
     if (!sessionStorage.getItem('mySessionVal')) { 
    //   ^^^ you can check time here as well ? 
      $('<div />', { 
       id : "alertmsg", 
       text : "Join our email list and receive the latest updates at 3Elements Review.", 
       on : { 
        click: function() { 
         $(this).slideUp(200); 
        } 
       } 
      }).appendTo('body').fadeIn("slow").delay(6000).fadeOut("slow"); 
      sessionStorage.setItem('mySessionVal', true); 
      sessionStorage.setItem('storedWhen', Date.now()); 
     } 
    } 
}); 

FIDDLE

+0

非常感謝。我注意到你淡出了毫秒。這如何影響幻燈片效果? –

+0

哦,甚至沒有注意到!如果我沒有記錯,'慢'應該等於600毫秒,'快'將是400,但無論使用什麼都不重要,只有動畫速度會改變。 – adeneo

+0

此外,點擊功能不再有效。 –

相關問題