每次访问仅运行一次

时间:2018-02-15 00:51:52

标签: jquery

我非常非常新。我在下面的代码中放置了这个函数的功能和位置,每次访问只运行一次?

<script>
jQuery('.count').each (function () {
    jQuery(this).prop('Counter',0).animate({
        Counter: jQuery(this).text()
    }, {
        duration: 4000,
        easing: 'swing',
        step: function (now) {
            jQuery(this).text(Math.ceil(now));
        }
    });
});
</script>

2 个答案:

答案 0 :(得分:0)

更新了答案。您可以尝试使用会话存储。

  if (!sessionStorage.firstVisit) {
        console.log('first time');
        //run your desired code here.

       sessionStorage.setItem("firstVisit", "1");
    }

答案 1 :(得分:0)

从这里开始: #a/1599367/6687923

包含JQuery.Сookie库(包含JQuery库之后):

<head>
// ...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
// ... 
</head>

使用此脚本:

<script>
if(!$.cookie("firstVisit"){

    // first time code
    // ...

    // Create expiring cookie, 2 days from then:
    $.cookie("firstVisit", "1", { expires: 2 });
}
</script>
相关问题