#1 by WD95
Hi, everyone, I want to track how long people stay on a page. But since there might be multiple submissions on the same page, I'm looking to log the time between each submission. Right now, my code can only capture the interval from the last submission. How should I tweak the code to make sure the previously stored interval data isn't overwritten? Thanks! <script> var enterTime = Date.now(); var submitIntervals = []; document.addEventListener('DOMContentLoaded', function() { var form = document.querySelector('form'); function updateTimeInput() { var timeInput = document.querySelector('input[name="time_on_page1"]'); if (!timeInput) { timeInput = document.createElement('input'); timeInput.type = 'hidden'; timeInput.name = 'time_on_page1'; form.appendChild(timeInput); } timeInput.value = JSON.stringify(submitIntervals); } form.addEventListener('submit', function(e) { var currentTime = Date.now(); var timeSpent = (currentTime - enterTime) / 1000; submitIntervals.push(timeSpent); enterTime = currentTime; updateTimeInput(); }); }); </script>