Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation I need a zero added for my Javascript countdown. so 9 looks like 09

    Hey Everyone,

    I'm happy to have joined this forum. I have a javascript countdown but the digits countdown like 14 13 12 11 10 9 8 7 ... and I want it to look like 14 13 12 11 10 09 08 07.... I need this done for Days Hours Minutes and Seconds.

    Here is the code. Thank you in advanced for your help.

    Code:
    <script type="text/javascript">
    function cd() {
    	var now = <?php echo $now; ?>;
    	var target = <?php echo $target; ?>;
    	var horizvert = '<?php echo $horizvert; ?>';
    	var daytext = '<?php echo $daytext; ?>';
    	var daystext = '<?php echo $daystext; ?>';
    	var hourtext = '<?php echo $hourtext; ?>';
    	var hourstext = '<?php echo $hourstext; ?>';
    	var minutetext = '<?php echo $minutetext; ?>';
    	var minutestext = '<?php echo $minutestext; ?>';
    	var secondtext = '<?php echo $secondtext; ?>';
    	var secondstext = '<?php echo $secondstext; ?>';
    	var whatnow = '<?php echo $whatnow; ?>';
    	var redirect = '<?php echo $redirect; ?>';
    	
    	timediff = target - now;
    	
    	var daysleft = 0;
    	var hoursleft = 0;
    	var minutesleft = 0;
    	var secondsleft = timediff;		
    
    	if (timediff >= 60) {
    		secondsleft = timediff % 60;
    		minutesleft = (timediff - secondsleft) / 60;
    	}
    
    	if (minutesleft >= 60) {
    		timediff = minutesleft;
    		minutesleft = timediff % 60;
    		hoursleft = (timediff - minutesleft) / 60;
    	}
    
    	if (hoursleft >= 24) {
    		timediff = hoursleft;
    		hoursleft = timediff % 24;
    		daysleft = (timediff - hoursleft) / 24;
    	}
    	
    	var gmctime = document.getElementById("gmctime");
    	var gmctimetext = '';
    	
    	var gmccountdown_timer = setInterval(gmcTimer, 1000);
    
    	function gmcUpdateDivHorizontal() {
    		gmctimetext = '';
    		gmctimetext += (daysleft) ? daysleft + (daysleft==1 ? ' '+daytext+' ' : ' '+daystext+' ') : '';
    		gmctimetext += (hoursleft || daysleft) ? hoursleft + (hoursleft==1 ? ' '+hourtext+' ' : ' '+hourstext+' ') : '';
    		gmctimetext += (minutesleft || hoursleft || daysleft) ? minutesleft + (minutesleft==1 ? ' '+minutetext+' ' : ' '+minutestext+' ') : '';
    		gmctimetext += secondsleft + (secondsleft==1 ? ' '+secondtext+' ' : ' '+secondstext+' ');
    		gmctime.innerHTML = gmctimetext;
    	}
    	
    	function gmcUpdateDivVertical() {
    		gmctimetext = '';
    		gmctimetext += (daysleft) ? daysleft + (daysleft==1 ? ' '+daytext+'<br />' : ' '+daystext+'<br />') : '';
    		gmctimetext += (hoursleft || daysleft) ? hoursleft + (hoursleft==1 ? ' '+hourtext+'<br />' : ' '+hourstext+'<br />') : '';
    		gmctimetext += (minutesleft || hoursleft || daysleft) ? minutesleft + (minutesleft==1 ? ' '+minutetext+'<br />' : ' '+minutestext+'<br />') : '';
    		gmctimetext += secondsleft + (secondsleft==1 ? ' '+secondtext+'<br />' : ' '+secondstext+'<br />');
    		gmctime.innerHTML = gmctimetext;
    	}
    	
    	function gmcTimer() {
    		if (secondsleft == 0 && minutesleft == 0 && hoursleft == 0 && daysleft ==0) {
    			clearInterval(gmccountdown_timer);
    			if (whatnow == 'text') {
    				document.getElementById('gmcpre').style.display = 'none';
    				document.getElementById('datetime').style.display = 'none';
    				document.getElementById('gmcpost').style.display = 'none';
    				document.getElementById('gmcafter').style.display = 'block';
    			} else {
    				window.location = redirect;
    			}
    			return;
    		}
    	
    		if (secondsleft > 0) secondsleft--;
    		else {
    			secondsleft = (minutesleft || hoursleft || daysleft) ? 59 : 0;
    			if (minutesleft > 0) minutesleft--;
    			else {
    				minutesleft = (hoursleft || daysleft) ? 59 : 0;
    				if (hoursleft > 0) hoursleft--;
    				else {
    					hoursleft = (daysleft) ? 23 : 0;
    					if (daysleft) daysleft--;
    				}
    			}
    		}
    		
    		if (horizvert == 'Horizontal') {
    			gmcUpdateDivHorizontal();
    		} else {
    			gmcUpdateDivVertical();
    		}
    	}
    }
    window.onload = cd;
    </script>
    Last edited by eclipsemedia; 12-01-2009 at 04:24 PM. Reason: better spelling

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    The way your script is written means that it cannot readily be modified as you wish. Here is an alternative, which if you insist on using your own script will show you the way to go about modifying it.


    Code:
    <script type = "text/javascript">
    
    var timeInSecs;
    var ticker;
    
    function startTimer(secs){
    timeInSecs = parseInt(secs);
    ticker = setInterval("tick()",1000); 
    tick(); // to start counter display right away
    }
    
    function tick() {
    var secs = timeInSecs;
    if (secs>0) {
    timeInSecs--;
    }
    else {
    clearInterval(ticker); // stop counting at zero
    //startTimer(60 * 60 *24 * 5);  // and start again if required
    }
    
    var days = Math.floor(secs/86400);
    secs %= 86400;
    var hours= Math.floor(secs/3600);
    secs %= 3600;
    var mins = Math.floor(secs/60);
    secs %= 60;
    
    var result = ((hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins
                      + ":" + ( (secs < 10) ? "0" : "" ) + secs;
    result = days + " Days: " + result;
    document.getElementById("countdown").innerHTML = result;
    }
    
    </script>
    
    </head>
    
    <body onload = "startTimer(60 * 60 * 24 * 5)">
    
    <span id="countdown" style="font-weight: bold;"></span>
    At its simplest:-

    Code:
    if (days <10) {days = "0" + days}
    but of course the variable days is then a string, not a number, so is no longer available for further calculations.

    Or you could do thus:-

    Code:
    if (hoursleft >= 24) {
    timediff = hoursleft;
    hoursleft = timediff % 24;
    daysleft = (timediff - hoursleft) / 24;
    daysleftdisplay = daysleft;
    if (daysleftdisplay <10) {daysleftdisplay = "0" + days)
    }
    and use daysleftdisplay and so on in the functions which generate the gmctimetext


    "Good judgment comes from experience and experience comes from bad judgment."
    Last edited by Philip M; 12-01-2009 at 04:46 PM.

  • #3
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for your reply. So there is no way the top script can be modified and copied and pasted with out doing other mods? I don't know enough of Javascript to rewrite the code I posted.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by eclipsemedia View Post
    Thank you for your reply. So there is no way the top script can be modified and copied and pasted with out doing other mods? I don't know enough of Javascript to rewrite the code I posted.
    See Post #2. The way your script is written means that it cannot readily be modified as you wish.

    Presumably you would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked.

  • #5
    New to the CF scene
    Join Date
    Dec 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Exactly. Thats why I found another machine with the leading zeros as needed. Thank you again


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •