In this, I am trying to assign the start time in the beginning and when I will click on the page it should return me the time spent but I am getting 0 always. I am not understanding that where I might have gone. Need your help.
var timeSpent = (function(){ var time = new Date(); var timeRun = false, startTime = time.getTime(); return function(){ // call this function by timeSpent()(); return time.getTime() - startTime; } })(); addEventListener("click", function(){ console.log(timeSpent()); })
timeandstartTimeare the same time. The instances of Date aren't live, they contain the time which was current at the time the instances were created.time.getTime()(which is when the user entered the page) with the current time