如果正文具有类,则仅显示一次弹出窗口(SweetAlert)

Show popup only once (SweetAlert) if body hasClass

目前我的 Wordpress 主题中有这段代码:

 jQuery(document).ready(function($) {

 if ($('body').hasClass('post-template-default')) {
    if(screen.width <= 991) {
        setTimeout(function(){ 
              swal({
        type: 'info',
        title: 'You can swipe!',
        text: 'Swipe to Right or Left to navigate through posts.',
        showConfirmButton: 'false',}) },1000); // alert }}});   

但我希望它 运行 只显示一次,之后不应再在网站上显示。

怎么办? :)

如果弹出窗口被查看一次,将数据保存到浏览器。这是基于您提供的代码的工作示例。这里我使用了sessionStorage.

if (!localStorage.getItem("popup")) {
  if ($('body').hasClass('post-template-default')) {
    if(screen.width <= 991) {
        setTimeout(function(){ 
          swal({
          type: 'info',
          title: 'You can swipe!',
          text: 'Swipe to Right or Left to navigate through posts.',
          showConfirmButton: 'false',}) 
          },1000
      ); 
  }
  localStorage.setItem("popup", 'viewed');
}

该代码非常有效,它不起作用,因为它是我在 if ( !is_user_logged_in() ){ ?> <script> ... </script> <?php } 函数中插入的。谢谢你的回答和时间:)