Panta Posted April 9, 2017 Share Posted April 9, 2017 please i have a function that gets time for me. Now i want to display the time twice on a page but is not displaying twice. it only displays just onces this is my code now when i repeat this <!--<span id="timeleftpay"> </span> it doesnt display again <script language="JavaScript"> <!-- var year = <?php echo $ydfx; ?>; // in what year will your target be reached? var month = <?php echo ($mdfx-1); ?>; // value between 0 and 11 (0=january,1=february,...,11=december) var day = <?php echo $ddfx; ?>; // between 1 and 31 var hour = <?php echo $hdfx; ?>; // between 0 and 24 var minute = <?php echo $idfx; ?>; // between 0 and 60 var second = <?php echo $sdfx; ?>; // between 0 and 60 var eventtext = " left to make payment"; // text that appears next to the time left var endtext = " made payment!!"; // text that appears when the target has been reached var end = new Date(year,month,day,hour,minute,second); function timeleftpay(){ var now = new Date(); if(now.getYear() < 1900) yr = now.getYear() + 1900; var sec = second - now.getSeconds(); var min = minute - now.getMinutes(); var hr = hour - now.getHours(); var dy = day - now.getDate(); var mnth = month - now.getMonth(); var yr = year - yr; var daysinmnth = 32 - new Date(now.getYear(),now.getMonth(), 32).getDate(); if(sec < 0){ sec = (sec+60)%60; min--; } if(min < 0){ min = (min+60)%60; hr--; } if(hr < 0){ hr = (hr+24)%24; dy--; } if(dy < 0){ dy = (dy+daysinmnth)%daysinmnth; mnth--; } if(mnth < 0){ mnth = (mnth+12)%12; yr--; } var sectext = " seconds "; var mintext = " minutes, and "; var hrtext = " hours, "; var dytext = " days, "; var mnthtext = " months, "; var yrtext = " years, "; if (yr == 1) yrtext = " year, "; if (mnth == 1) mnthtext = " month, "; if (dy == 1) dytext = " day, "; if (hr == 1) hrtext = " hour, "; if (min == 1) mintext = " minute, and "; if (sec == 1) sectext = " second "; if(now >= end){ document.getElementById("timeleftpay").innerHTML = endtext; clearTimeout(timerID); } else{ document.getElementById("timeleftpay").innerHTML = dy + dytext + hr + hrtext + min + mintext + sec + sectext + eventtext; } timerID = setTimeout("timeleftpay()", 1000); } window.onload = timeleftpay; //--> </script> <!--<span id="timeleftpay"> </span> Link to comment Share on other sites More sharing options...
dsonesuk Posted April 9, 2017 Share Posted April 9, 2017 (edited) Ids name must be unique and only used once within a page, if used more than once, only one will actually work. Edited April 9, 2017 by dsonesuk Link to comment Share on other sites More sharing options...
Panta Posted April 9, 2017 Author Share Posted April 9, 2017 7 minutes ago, dsonesuk said: Ids name must be unique and only used once within a page, if used more than once, only one will actually work. so how do i fix this in my code? Link to comment Share on other sites More sharing options...
dsonesuk Posted April 9, 2017 Share Posted April 9, 2017 (edited) Use different id for second and just use same code to add content OR use classname instead and use var parentClass=document.getElementsByClassName("timeleftpay"); for(var i=0;i<parentClass.length;i++){ parentClass[i].innerHTML = dy + dytext + hr + hrtext + min + mintext + sec + sectext + eventtext; } Use <span class="timeleftpay"> </span> Edited April 9, 2017 by dsonesuk 1 Link to comment Share on other sites More sharing options...
Panta Posted April 9, 2017 Author Share Posted April 9, 2017 2 hours ago, dsonesuk said: Use different id for second and just use same code to add content OR use classname instead and use var parentClass=document.getElementsByClassName("timeleftpay"); for(var i=0;i<parentClass.length;i++){ parentClass[i].innerHTML = dy + dytext + hr + hrtext + min + mintext + sec + sectext + eventtext; } Use <span class="timeleftpay"> </span> Thanks brother . it worked Link to comment Share on other sites More sharing options...
sandeepm Posted April 14, 2017 Share Posted April 14, 2017 Instead of using window.onload use below code $(document).ready(function() { timeleftpay(); }); Link to comment Share on other sites More sharing options...
dsonesuk Posted April 14, 2017 Share Posted April 14, 2017 2 hours ago, sandeepm said: Instead of using window.onload use below code $(document).ready(function() { timeleftpay(); }); Depending IF the poster is already using jquery as all i see is plain vanilla JavaScript code, that would overkill just for one small line of code. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now