wilna Posted October 14, 2011 Share Posted October 14, 2011 Hi, please excuse my lack of knowledge. I've got a JavaScript clock on my page. My problem is when the users refreshes my php page the clock starts from 0 again. I need the clock to remember the value and then just continue from where it was. I've been trying with cookies but gave up. Can someone please help me? I've been trying but I'm clueless. Javascript var pageVisisted = new Date(); setInterval(function() { var timeOnSite = new Date() - pageVisisted; var secondsTotal = timeOnSite / 1000; var hours = Math.floor(secondsTotal / 3600); var minutes = Math.floor(secondsTotal / 60) % 3600; var seconds = Math.floor(secondsTotal) % 60; document.getElementById('counter').innerHTML = hours + ":" + minutes + ":" + seconds; }, 1000); The php page <head> <?php session_start(); ?> <script type="text/javascript" src="counter.js"></script> </head> <body> <?php echo "<span id='counter'></span>"; ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/249112-javascript-clock-on-refresh/ Share on other sites More sharing options...
Andy-H Posted October 14, 2011 Share Posted October 14, 2011 function setCookie(c_name,value,exdays) { if ( typeof exdays == 'undefined' ) var exdays = 365; var exdate = new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value = escape(value) +";expires="+ exdate.toUTCString()); document.cookie=c_name + "=" + c_value; } function getCookie(c_name) { var i, x, y, cookies=document.cookie.split(";"); for (i = 0; i < cookies.length; i++) { x = cookies[i].substr(0, cookies[i].indexOf("=") ); y = cookies[i].substr(cookies[i].indexOf("=") + 1); x = x.replace(/^\s+|\s+$/g, ""); if (x==c_name) { return unescape(y); } } return false; } var pageVisisted = getCookie('pageVisited'); if ( !pageVisited ) pageVisited = new Date(); setInterval(function() { var timeOnSite = new Date() - pageVisisted; var secondsTotal = timeOnSite / 1000; var hours = Math.floor(secondsTotal / 3600); var minutes = Math.floor(secondsTotal / 60) % 3600; var seconds = Math.floor(secondsTotal) % 60; document.getElementById('counter').innerHTML = hours + ":" + minutes + ":" + seconds; }, 1000); window.onunload = setCookie('pageVisited', pageVisited); <head> <?php session_start(); ?> <script type="text/javascript" src="counter.js"></script> </head> <body> <span id='counter'></span> </body> Quote Link to comment https://forums.phpfreaks.com/topic/249112-javascript-clock-on-refresh/#findComment-1279292 Share on other sites More sharing options...
wilna Posted October 14, 2011 Author Share Posted October 14, 2011 Thank you, I will test now and report back. Quote Link to comment https://forums.phpfreaks.com/topic/249112-javascript-clock-on-refresh/#findComment-1279308 Share on other sites More sharing options...
wilna Posted October 14, 2011 Author Share Posted October 14, 2011 First of all thank you for giving me the code. But unfortunately it's not displaying the clock. Quote Link to comment https://forums.phpfreaks.com/topic/249112-javascript-clock-on-refresh/#findComment-1279318 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.