如何在10秒钟后通过javascript设置cookie [英] how to set cookie after 10 seconds by javascript

查看:59
本文介绍了如何在10秒钟后通过javascript设置cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要您解决此问题.我尝试了几种方法,但是我的脑子不再起作用了.我做了一个在 JavaScript 中设置cookie的功能,但是那样,当窗口加载时,将立即设置立即的cookie( visit ),但是我需要在用户停留在网站页面上的 10秒后设置Cookie.我的朋友,你愿意帮我吗?
另外,通过此代码,我想显示一个 Modal ,并且 hereiakarneta 是该Modal的 ID .

I need you really to solve this issue. I tried several ways but my mind does not work anymore. I made a function to set cookie in JavaScript but in that way, when the window loads, the cookie (visit) immediately will be set, but I need the cookie to be set after 10 seconds that user stay in page of website. would you help me, my freinds?
In addition by this code, I want to show a Modal and hereiakarneta is the ID of that Modal.

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

    function getCookieVal(offset) {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1)
            endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }

    function GetCookie(name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                return getCookieVal(j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0)
                break;
        }
        return null;
    }

    function SetCookie(name, value) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (2 < argc) ? argv[2] : null;
        var path = (3 < argc) ? argv[3] : null;
        var domain = (4 < argc) ? argv[4] : null;
        var secure = (5 < argc) ? argv[5] : false;
        document.cookie = name + "=" + escape(value) +
            ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
            ((path == null) ? "" : ("; path=" + path)) +
            ((domain == null) ? "" : ("; domain=" + domain)) +
            ((secure == true) ? "; secure" : "");
    }

    function DisplayInfo() {
        var expdate = new Date();
        var visit;
        expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
        if (!(visit = GetCookie("visit")))
            visit = 0;
        visit++;
        SetCookie("visit", visit, expdate, "/", null, false);
        if (visit == 1) {
            $('#hereiakarneta').modal({ show: true });
        }
        if (visit == 2) {
            $('#hereiakarneta').modal({ show: true });
        }
        if (visit == 3) {
            $('#hereiakarneta').modal({ show: true });
        }
    }

    //window.onload = DisplayInfo
    $(window).on("load", DisplayInfo);

});  

HTML

<!-- Modal -->
<div id="hereiakarneta" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title" style="text-align: center;" >Download on app store</h4>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-lg-12"><img src="" class="img-responsive" /></div>
        </div>
        <div class="row">
          <div class="col-xs-4 col-xs-offset-2"><img src="" class="img-responsive" /></div>
          <div class="col-xs-4"><img src="" class="img-responsive" /></div>
        </div> 
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>  

非常感谢大家

推荐答案

最后,我发现10秒钟后我无法将+1实时添加到cookie(没有刷新页面).因此,我更改了我的问题,以另一种方式显示模式(弹出窗口).所以请参阅新问题:

Finally I found that I can not add +1 to cookie on live (without refreshing page) after 10 seconds. So I changes My question to show the modal (popup) by another way. so please see the new question:
How to Add +1 to a cookie value on click of a button

在更改我的问题之前,我将以下代码与 setTimeout()一起使用,但问题是:当用户打开一页时,将立即设置cookie,并在10秒后将模式设置为显示,因此,当用户在10秒之前离开页面时,我想向他展示模式的那3次之一将丢失:|我需要当用户打开页面时,在10秒钟后设置cookie,而当使用者在10秒钟前离开页面时,不设置cookie.

Before changing my question, I used the following code with setTimeout() but the problem was: when the user open one page, immediately the cookie will be set and after 10 seconds the modal will be show, so when the user before 10 second leave the page, one of those 3 times I want to show him the modal will be lost :| I needed that when a user open the page, after 10 seconds the cookie be set and when the use leave the page before 10 seconds, the cookie not be set.

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

    function getCookieVal(offset) {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1)
            endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }

    function GetCookie(name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
            var j = i + alen;
            if (document.cookie.substring(i, j) == arg)
                return getCookieVal(j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0)
                break;
        }
        return null;
    }

    function SetCookie(name, value) {
        var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (2 < argc) ? argv[2] : null;
        var path = (3 < argc) ? argv[3] : null;
        var domain = (4 < argc) ? argv[4] : null;
        var secure = (5 < argc) ? argv[5] : false;
        document.cookie = name + "=" + escape(value) +
            ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
            ((path == null) ? "" : ("; path=" + path)) +
            ((domain == null) ? "" : ("; domain=" + domain)) +
            ((secure == true) ? "; secure" : "");
    }

    function DisplayInfo() {
        var expdate = new Date();
        var visit;
        expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
        if (!(visit = GetCookie("HereIsKarneta")))
            visit = 0;
        visit++;
        SetCookie("HereIsKarneta", visit, expdate, "/", null, false);
        //var message;
        if (visit < 4) {
            //$('#hereiakarneta').modal({ show: true });
            setTimeout(function(){
                $('#hereiakarneta').modal({
                    show: true
                })
            }, 2000);
        }
        if (visit >= 4) {
            $(".dologinfirst").delay(2000).fadeIn(500);
            $("#menubutton").click(function(){
                $(".dologinfirst").hide();
            });
            $('body').click(function() {
                $(".dologinfirst").hide();
            });
        }
    }

    //window.onload = DisplayInfo
    $(window).on("load", DisplayInfo);

});

这篇关于如何在10秒钟后通过javascript设置cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆