繁体   English   中英

我将如何实施柜台

[英]how would I implement a counter

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

        }

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

您正在为时间戳和访问的计数使用相同的键surveyTimestamp 使用不同的键(例如surveyTimestampsurveyVisitedCount会很不错)

我认为有一种更简单的方法可以实现此目的,而无需使用Date()或cookie。

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

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM