2015-11-05 18 views
0
    //  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); 

     } 

我想添加一個橫幅與計數器。當用戶在一段時間內來到網站時,他們被要求填寫調查表。 問題是我似乎無法讓櫃檯增加。我可能做錯了什麼。謝謝。我將如何實現一個計數器

回答

0

對於時間戳和訪問計數都使用了相同的密鑰surveyTimestamp。使用不同的密鑰(說surveyTimestampsurveyVisitedCount,你會很好)

+0

伊夫改變它,它仍然來,只有使用增量因爲背部不確定 – ronoc4

0

有一個更簡單的方法來實現這一點,我認爲會做你想做的,而不使用Date()或餅乾。

JavaScript具有各種定時功能,其中之一是setTimeOut()。這會每n毫秒重複執行一次功能。使用這種方法,我們只需要將計時器變量初始化爲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. 
+0

林我設置了一個cookie,並每隔15秒檢查一次,看看價值是否上漲。當它到達3時,它停止並顯示一個調查 – ronoc4

+0

你應該可以用上面的例子來做到這一點。我修改了示例代碼以顯示此內容。 – AndroidNoobie

相關問題