【问题标题】:How to call a jquery function once every 24 hours?如何每24小时调用一次jquery函数?
【发布时间】:2013-09-04 17:35:24
【问题描述】:

我正在努力在我的网站上制作横幅,以告知访问者有关 Cookie 的信息。我想使用 jquery 应用此横幅,当人们单击“确定”时,它应该会消失。那部分我可以做,但如果他们在 24 小时后再次访问该网站,我希望它重新出现。我想我可以用 cookie 做到这一点,但我不太确定如何去做。谁能指出我正确的方向?

【问题讨论】:

  • Cookie 是您最好的选择,但如果人们将其关闭,它将无法正常工作。
  • 我在 jQuery 部门帮不上忙,但我会小心每 24 小时显示一次横幅,因为你会惹恼很多人并可能赶走他们。在他们已经接受 cookie 政策后显示横幅的目的是什么?
  • 我同意@Tim,这是一个用户体验设计缺陷。
  • 您的标题令人困惑,这就是您得到不相关答案的原因。
  • @JeppeWillesenSmith 这个问题还没有解决吗?如果没有,请关闭问题(通过选择最佳答案)并投票支持任何对您有帮助的答案。如果您还没有足够的代表,您可以稍后再回来执行此操作。如果您希望微调您的问题,通常最好关闭原始问题并提出一个新问题(新问题将出现在列表顶部,并具有最大的曝光率)。

标签: jquery cookies


【解决方案1】:

JQuery 没有内置的 cookie 处理程序,但在常规 js 中确实没有那么难。正如@Tim 所提到的,这是糟糕的用户体验设计,你不应该用这样的弹出窗口来惹恼人们。一次就够了。但我理解为什么您可能希望确保它不会每次都弹出。如果您愿意,您可以将日期设置为一个非常遥远的日期,以帮助防止它再次出现,直到用户清除那里的 cookie。

这样设置cookie

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

然后像这样检索它。

function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
  {
  c_start = c_value.indexOf(c_name + "=");
  }
if (c_start == -1)
  {
  c_value = null;
  }
else
  {
  c_start = c_value.indexOf("=", c_start) + 1;
  var c_end = c_value.indexOf(";", c_start);
  if (c_end == -1)
  {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}

你可以像这样使用整个东西......

function checkCookie()
{
var username=getCookie("username");
  if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else 
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("username",username,365);
    }
  }

*直接取自这里http://www.w3schools.com/js/js_cookies.asp* }

【讨论】:

  • 虽然这对于 cookie 来说是一个很好的答案,但请避免使用 w3schools 以供将来参考。
  • 为什么?它是有效的不是吗?我的意思是如果不允许引用之类的,我可以更简单地重写它。
  • w3 作为一个组织谴责它,因为它会传播不良信息,同时还通过公牛**** 认证从用户的无知中获利。引用更合适和合法的来源只是一个好的做法。 w3fools.com
  • 啊。明白了。我只是不时将它用作“文档”来源,并像这样提取快速信息。
【解决方案2】:

我会使用 localStorage,在点击时添加当前日期:

var curDate = new Date();

$('#Button').on('click',function(){
    localStorage['banner'] = curDate.getDate();
});

然后检查是否存在,并比较日期:

if(localStorage['banner'].length > 0){
    var future = new Date();
    future.setDate(localStorage['banner'] + (24 * 60 * 60 * 1000));

    if(future > curDate){
        localStorage['banner'].length = 0;
    }
}

如果他们使用相同的浏览器,这将起作用,就像 cookie 一样。请注意,这仅适用于 IE8 及更高版本,仅涵盖全球 99.2% 的用户。

【讨论】:

  • 本地存储很不错,但不完全跨平台兼容。
  • 99.2% 非常具体。 =P
  • 这是半个笑话......它还基于一些研究网站,表明全球 IE6 和 IE7 的使用率
【解决方案3】:

对于更少的代码,更简单的实现试试这个:

jquery.cookie

例子:

创建一个 cookie:

$.cookie('the_cookie', 'the_value');

读取 cookie:

var cookieval = $.cookie('the_cookie');

创建一个即将过期的 cookie(7 天后过期):

$.cookie('the_cookie', 'the_value', { expires: 7 });

Source

【讨论】:

    【解决方案4】:

    您可以通过编写一个非常简单的freshVistor(howFresh) 函数来解决这个问题,该函数接受一个可选参数来定义您认为“新鲜”的新鲜程度。

    function freshVisitor(howFresh) {
        var namespace = 'freshVisitorSOCookie';
        if (readCookie(namespace)) {
            return false;
        }
        return makeCookie(namespace, 'yes', {
            expires: howFresh || 1
        });
    }
    

    您的应用程序可以像这样使用freshVisitor() 函数:

    if (freshVisitor()) {
        /* This means the user is new or has not visited within 1 day
         * So show your banner or do something that you want to do */
    } else {
        /* The user has visited already within the previous day
         * or has disabled cookies. */
    }
    

    你也可以指定你自己对“新鲜”的定义:

    if (freshVisitor(2)) ... /* uses a 2-day period to calculate freshness */
    

    请注意!为了用户友好,我在用户禁用 cookie 时使此功能自动返回 false,以防止没有 cookie 的人被横幅广告淹没。我相信这是对待访问我网站的人的正确方式,但如果您希望扭转这种尊重行为,请随时调整我的 freshVisitor() 代码中的逻辑。

    当然,您需要适当的 cookie 函数来使用上面的表示法计算到期日期,所以这里是完整的代码:

    <script>
        function makeCookie(name, value, p) {
            var s, k;
    
            function reldate(days) {
                var d;
                d = new Date();
                d.setTime(d.getTime() + days * 86400000);
                return d.toGMTString();
            }
            s = escape(name) + '=' + escape(value);
            if (p)
                for (k in p) {
    
                    /* convert a numeric expires value to a relative date */
    
                    if (k == 'expires')
                        p[k] = isNaN(p[k]) ? p[k] : reldate(p[k]);
    
                    /* The secure property is the only special case
    here, and it causes two problems. Rather than
    being '; protocol=secure' like all other
    properties, the secure property is set by
    appending '; secure', so we have to use a
    ternary statement to format the string.
    
    The second problem is that secure doesn't have
    any value associated with it, so whatever value
    people use doesn't matter. However, we don't
    want to surprise people who set { secure: false }.
    For this reason, we actually do have to check
    the value of the secure property so that someone
    won't end up with a secure cookie when
    they didn't want one. */
    
                    if (p[k])
                        s += '; ' + (k != 'secure' ? k + '=' + p[k] : k);
                }
            document.cookie = s;
            return readCookie(name) == value;
        }
    
        function readCookie(name) {
            var s = document.cookie,
                i;
            if (s)
                for (i = 0, s = s.split('; '); i < s.length; i++) {
                    s[i] = s[i].split('=', 2);
                    if (unescape(s[i][0]) == name)
                        return unescape(s[i][1]);
                }
            return null;
        }
    
        function removeCookie(name) {
            return !makeCookie(name, '', {
                expires: -1
            });
        }
    
        function freshVisitor(howFresh) {
            var namespace = 'freshVisitorSOCookie';
            if (readCookie(namespace)) {
                return false;
            }
            return makeCookie(namespace, 'yes', {
                expires: howFresh || 1
            });
        }
    
        document.write('Fresh visitor status: ', freshVisitor());
    </script>
    

    【讨论】:

      猜你喜欢
      • 2020-09-11
      • 2021-01-09
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-15
      相关资源
      最近更新 更多