我可以在以下弹出脚本中添加什么,使其每天仅显示一次?

问题描述

这是我的脚本,位于</body>上方。它显示每次用户访问我的博客上的链接时。我希望它每天只显示一次,以免打扰我的访客。我做了几天实验,但未能实现我的目标。如果有人可以帮忙吗?

<div class='demo-float'>
<span class='df-hide'><i class='fas fa-times'/></span>
<div class='df-logo'/>
<h3>Social Media Masterplan</h3>
<p class='excerpt'>Easy to follow Social Media post ideas designed to grow your business and give you competitive advantage.</p>
<a class='button download colored-button' href='https://vandeweybalao.blogspot.com/2020/06/social-media-content-calendar-for-busy-entrepreneurs.html' target='_blank' title='Social Media Masterplan Toolkit'>DOWNLOAD TOOLKIT</a>
</div>
<script type='text/javascript'>
$(function(){$(&#39;.df-hide&#39;).on(&#39;click&#39;,function(){$(&#39;.demo-float&#39;).fadeOut(170);});});
</script>

解决方法

根据应用程序的组成方式,有两种处理方法。如果您的应用程序没有服务器端,则可以在js中使用localStorage属性(全局对象)来检查用户当天是否访问了您的页面。这样做的方式是:

function hasOneDayPassed(){
  var date = new Date().toLocaleDateString();

  if( localStorage.getItem("savedDate") == date ) 
      return false;

  localStorage.setItem("savedDate",date);
  return true;
}

function runOncePerDayMyCode(){
  if( !hasOneDayPassed() ) return false;

  // your code below
  document.getElementById("myDiv").style.display = "block";//or whatever
}


runOncePerDayMyCode();

如果应用程序具有服务器端,则可以保存IP地址和DateTime(或登录凭据)之类的日志,并根据您的后端信息显示或不显示多余的内容