how to set cookie after 10 seconds by javascript

人盡茶涼 提交于 2021-01-29 10:14:08

问题


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>  

Thanks a lot to all of you


回答1:


Add a timeout to your page's load-event like this:

window.addEventListener('load',function(){
  setTimeout(function(){
    document.cookie = "hasBeenHereFor10Seconds=true";
  },10000)
});



回答2:


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

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);

});


来源:https://stackoverflow.com/questions/61661918/how-to-set-cookie-after-10-seconds-by-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!