Jump to content
Panta

window.load not displaying my record

Recommended Posts

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>


 

 

Share this post


Link to post
Share on other sites

Ids name must be unique and only used once within a page, if used more than once, only one will actually work.

Edited by dsonesuk

Share this post


Link to post
Share on other sites
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? 

Share this post


Link to post
Share on other sites

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 by dsonesuk
  • Like 1

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...