SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    May 2006
    Posts
    457
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    countdown time added to label on button click

    Hello all, I have some javascipt coundown timer that will begin on page load. Is there any way of altering this so that the counter will display within a label whan a button is clicked?

    Code:
    <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
    
    dateFuture = new Date(2006,11,7,10,27,12);
    
    function GetCount(){
    
    	dateNow = new Date();									//grab current date
    	amount = dateFuture.getTime() - dateNow.getTime();		//calc milliseconds between dates
    	delete dateNow;
    
    	// time is already past
    	if(amount < 0){
    		document.getElementById('countbox').innerHTML="Now!";
    	}
    	// date is still good
    	else{
    		days=0;hours=0;mins=0;secs=0;out="";
    
    		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs
    
    		days=Math.floor(amount/86400);//days
    		amount=amount%86400;
    
    		hours=Math.floor(amount/3600);//hours
    		amount=amount%3600;
    
    		mins=Math.floor(amount/60);//minutes
    		amount=amount%60;
    
    		secs=Math.floor(amount);//seconds
    
    		if(days != 0){out += days +" day"+((days!=1)?"s":"")+", ";}
    		if(days != 0 || hours != 0){out += hours +" hour"+((hours!=1)?"s":"")+", ";}
    		if(days != 0 || hours != 0 || mins != 0){out += mins +" minute"+((mins!=1)?"s":"")+", ";}
    		out += secs +" seconds";
    		document.getElementById('countbox').innerHTML=out;
    
    		setTimeout("GetCount()", 1000);
    	}
    }
    
    window.onload=function(){GetCount();}//call when everything has loaded
    
    </script>
    
    
    <div id="countbox"></div>
    Thanks in advanced

  2. #2
    SitePoint Enthusiast panthro's Avatar
    Join Date
    Dec 2006
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Bewise,

    Try the code below, I think that is what you need :

    Code:
    <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
    
    dateFuture = new Date(2006,11,7,10,27,12);
    
    function GetCount(){
    
    	dateNow = new Date();									//grab current date
    	amount = dateFuture.getTime() - dateNow.getTime();		//calc milliseconds between dates
    	delete dateNow;
    
    	// time is already past
    	if(amount < 0){
    		document.getElementById('button_object').value="Now!";
    	}
    	// date is still good
    	else{
    		days=0;hours=0;mins=0;secs=0;out="";
    
    		amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs
    
    		days=Math.floor(amount/86400);//days
    		amount=amount&#37;86400;
    
    		hours=Math.floor(amount/3600);//hours
    		amount=amount%3600;
    
    		mins=Math.floor(amount/60);//minutes
    		amount=amount%60;
    
    		secs=Math.floor(amount);//seconds
    
    		if(days != 0){out += days +" day"+((days!=1)?"s":"")+", ";}
    		if(days != 0 || hours != 0){out += hours +" hour"+((hours!=1)?"s":"")+", ";}
    		if(days != 0 || hours != 0 || mins != 0){out += mins +" minute"+((mins!=1)?"s":"")+", ";}
    		out += secs +" seconds";
    		document.getElementById('button_object').value=out;
    
    		setTimeout("GetCount()", 1000);
    	}
    }
    
    
    
    </script>
    
    
    <input type="button" value="click me" onclick="GetCount();" id="button_object" />


    Things to do When Bored

    My List of Classic Kids TV - Includes links to fansites


Bookmarks

Posting Permissions

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