使用Javascript自动增加会话无效

时间:2015-01-08 12:43:29

标签: javascript jquery

我需要什么

  • 我需要在用户refesh页面时我需要增加会话变量的数量。

js code

             <script type="text/javascript">
            var count=0;
            var counter=sessionStorage.setItem("count", 0);
            var counters= sessionStorage.getItem("count");
            console.log(counters);
            var page_count =counters;
            if (page_count == 4)
            {
            dataLayer.push({'event':'mobilePromo-android'});
            }
        $(document).ready(function()
        {
            var page_count =counters;
            var height= $(window).height();
            if (page_count == 4 )
            {

                $.ajax({
                    type: "GET",
                    url: "http://domain.com/mobilepopuptracker?from=android", 

                });

                $('body').html('<div class="row flush aligncenter popbx" style="height:'+height+'px"><img src="http://im.gifbt.com/images/logo-300p");              



            }
            else
            {

            }

               counters=counters+1;
        });
            function redirect()
            {

                  var a=$(location).attr('href');
                   window.location.href=a;
            }

        </script>

问题

  • 计数值不是自动增量,因为浏览器已刷新。
  • console.log(counter)// 0。
  • 欢迎提出任何建议。

在价值解决方案代码仍未运行之后

            var counter = null;
            if(sessionStorage.getItem("count") == null)
            {
            counter=sessionStorage.setItem("count", 0);
            counters = 0;
            }
            else
            {
            counters= parseInt(sessionStorage.getItem("count"));

            }

            var page_count =counters;
            if (page_count == 4)
            {
            dataLayer.push({'event':'mobilePromo-android'});
            }
            var page_count =counters;
            console.log(page_count);
            var height= $(window).height();
            if (page_count == 4 )
            {

                $.ajax({
                    type: "GET",
                    url: "http://times.com/mobilepopuptracker?from=android", 

                });

                $('body').html('<div class="row flush aligncenter popbx" style="height:'+height+'px">');                



            }
            else
            {

            }
              counters++;
             console.log(counters);
            function redirect()
            {

                  var a=$(location).attr('href');
                   window.location.href=a;
            }
  • 问题输出直到1,它不会被输入。有什么不对。

输出

  • 如果counter设置为0,则它​​将自动递增为1.依此类推。
  • console.log(计数器)// 01它应该是自动增量。

参考链接

  https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Storage

2 个答案:

答案 0 :(得分:2)

您需要先检查会话值是否可用

var counter = null;
if(sessionStorage.getItem("count") == null){
  counter=sessionStorage.setItem("count", 1);
  counters = 1;
}else{
  counters= parseInt(sessionStorage.getItem("count")); 
  counters++;
   counter=sessionStorage.setItem("count", counters);
}

答案 1 :(得分:0)

这样的事情应该有效

$(document).ready(function() {
    var ls = sessionStorage.getItem("session-count");
    if(ls) {
        /*Do whatever here*/
        var count = sessionStorage.getItem("session-count");
        sessionStorage.setItem("session-count", parseInt(count) + 1);           
    } else {
        sessionStorage.setItem("session-count", 1);
    }
});