Javascript:对于setInterval()函数内的变量,将计数递增5

时间:2014-02-21 18:54:32

标签: javascript google-analytics

我正在尝试使用Google Analytics events更准确地跟踪在网站上停留的时间(不依赖于访问网站上其他网页之间的增量时间)。我正在使用setInterval()甚至每5秒连续触发GA。如何正确增加与GA事件数据一起发送的秒数?

  var count = 0;
  setInterval(function(){
    // increment "count" by 5 each time setInterval is run
    ga('send', 'event', 'time', 'tracking', 'seconds', count);
  }, 5000);

4 个答案:

答案 0 :(得分:2)

  var count = 0;
  setInterval(function(){
    // increment "count" by 5 each time setInterval is run
    count+=5; //is this what you need?
    ga('send', 'event', 'time', 'tracking', 'seconds', count);
  }, 5000);

答案 1 :(得分:1)

这是你想要的吗?

var count = 0;
setInterval(function(){
  count = count + 5;
  // increment "count" by 5 each time setInterval is run
  ga('send', 'event', 'time', 'tracking', 'seconds', count);
}, 5000);

答案 2 :(得分:1)

这个怎么样?

var count = 0;
setInterval(function(){
    count+=5;
    // increment "count" by 5 each time setInterval is run
    ga('send', 'event', 'time', 'tracking', 'seconds', count);
}, 5000);

答案 3 :(得分:0)

不确定我是否正确收集,但我们可以这样做: var count = 0,interval=5000; setInterval(function(){ // increment "count" by 5 each time setInterval is run ga('send', 'event', 'time', 'tracking', 'seconds', count+interval); }, interval);

在清除间隔中,您可以再次设置count = 0。