Javascript 我如何实现计数器 //var-GAP=1000*60*60*8; var间隙=1000*10; var visted=$cookies.get('surveyVisitedCount'); var timestamp=new Date().getTime(); var oldtime=$cookies.get('surveyTimestamp'); 如果(旧时间!==未定义){ if((时间戳-间隙)>parseInt(oldtime,10)){ //增量 log('需要增加!'); //增量访问cookie,然后检查它是否超过3 如果(访问

Javascript 我如何实现计数器 //var-GAP=1000*60*60*8; var间隙=1000*10; var visted=$cookies.get('surveyVisitedCount'); var timestamp=new Date().getTime(); var oldtime=$cookies.get('surveyTimestamp'); 如果(旧时间!==未定义){ if((时间戳-间隙)>parseInt(oldtime,10)){ //增量 log('需要增加!'); //增量访问cookie,然后检查它是否超过3 如果(访问,javascript,angularjs,cookies,increment,Javascript,Angularjs,Cookies,Increment,我正在尝试添加一个带有计数器的横幅。当用户在一定时间内访问该站点时,会要求他们填写一份调查。 问题是我似乎无法使计数器一直递增。我可能做错了什么。谢谢。您对时间戳和访问次数使用相同的键surveyTimestamp。使用不同的键(比如surveyTimestamp和surveyVisitedCount你会很好)有一种更简单的方法来实现这一点,我认为它可以满足你的需要,而不必使用Date()或cookies JavaScript具有各种计时功能,其中之一是setTimeOut()。这将每隔n毫秒重

我正在尝试添加一个带有计数器的横幅。当用户在一定时间内访问该站点时,会要求他们填写一份调查。
问题是我似乎无法使计数器一直递增。我可能做错了什么。谢谢。

您对时间戳和访问次数使用相同的键
surveyTimestamp
。使用不同的键(比如
surveyTimestamp
surveyVisitedCount
你会很好)

有一种更简单的方法来实现这一点,我认为它可以满足你的需要,而不必使用
Date()
或cookies

JavaScript具有各种计时功能,其中之一是
setTimeOut()
。这将每隔
n
毫秒重复执行一个函数。使用这种方法,我们只需要将计时器变量初始化为0,并按所需的间隔递增它。例如,如果我们希望每秒递增一次,然后在达到某个值(即45秒)时停止:


我更改了它,但它仍然只使用增量返回Unfinedim,因为我设置了一个cookie,它每15秒检查一次,看值是否上升。当它达到3时,它停止并显示一个调查结果。你也应该能够通过上面的例子来做到这一点。我修改了示例代码来说明这一点。
                    //      var GAP = 1000 * 60 * 60 * 8;
            var GAP = 1000 * 10;

            var visted = $cookies.get('surveyVisitedCount');

            var timestamp = new Date().getTime();
            var oldtime = $cookies.get('surveyTimestamp');

            if(oldtime !== undefined) {
                if((timestamp - GAP) > parseInt(oldtime, 10)) {
                    // increment
                    console.log('Need to increment!');
                    // increment visits cookie, then check if it's past 3
                    if (visted < 3){
                        $cookies.put('surveyVisitedCount', visted++);
                        console.log('visted1' , visted);
                    } else {
                         //we add the banner
                        console.log('we add the banner');
                        console.log('visted2' , visted);
                    }

                }else{
                    console.log('dont need to increment');
                }
            }

            $cookies.put('surveyTimestamp', timestamp);

        }
var timerValue = 0; // Initialize timer to 0.
var timer;

function startTimer(timerValue) {
    var nextIncrement = function() {
        incrementTimer(timerValue);
    };
    timer = setTimeout(nextIncrement, 1000);    // Increments every second (1000 ms = 1 s)
} 

function incrementTimer(timerValue) {
    timerValue = timerValue + 1;    // This does the actual incrementing.
    startTimer(timerValue); // Pass the current timer value to startTimer for next increment.
    console.log(timerValue);    // Print values out to the console.

    // ADDED
    if (timerValue == 45) {
        clearTimeout(timer);    // Stop the timer.
        console.log("Show survey.");    // Show the survey.
    }
    // 

}

startTimer(timerValue); // Kick off the timer.