I am using bookblock.js and am trying to integrate a timer to count how many seconds a user spends on each slide/page. I have managed to include the timer, but how do I reset the timer each time the user lands on a new slide/page? I will also need to push the time spent in seconds to GTM dataLayer.
Below is my code for reference:
onBeforeFlip : function(page) {
var name = $(".bb-item").filter(function() { return $(this).css("display") == "block" }).attr("id");
var startTimer = setInterval(function(){
//console.log("seconds spent on this page");
}, 1000);
var d = new Date(1000*Math.round(startTimer/1000));
function pad(i) { return ('0'+i).slice(-2); }
var seconds = d.getUTCHours() + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds());
console.log(seconds);
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'event': 'slide',
'slidename': name,
'timespent': seconds
});
}
Thanks in advance!