Disable Javascript countdown function via click...

Posted on 2009-04-15
Last Modified: 2012-05-06
I have a simple Javascript countdown and redirect function on a page, is there a way to disable/enable the function via a click on the web page either with a "a href" or form button without refreshing the page?

And also...can I set a TARGET on the redirect to it opens the new page in a new window?


var start=new Date();


	var counts=10;

	function CountDown(){

		var now=new Date();


		var x=parseInt(counts-(now-start),10);

		if(document.form1){document.form1.clock.value = x;}


			timerID=setTimeout("CountDown()", 100)





//  End -->








Open in new window

Question by:Mitchel-T
    LVL 13

    Accepted Solution

    What about something like the following?

    Instead of using "setTimeout" you can use "setInterval" which will run a function once for every set interval. So if you set an interval of 1000 (i.e. 1 second) then your function will be executed once each second. There is no need to measure the amount of time elapsed since the last call because it will have been one second.

    Each time the handler is run, increment your counts variable.
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "">
    		<title>Demo Page</title>
    		<script type="text/javascript">
    			var countDownID, counts = 0, maxSeconds = 10;
    			function stopCountDown() {
    				if (countDownID) {
    					countDownID = null;
    			function updateClock() {
    				var clock = document.getElementById('clock');
    				if (clock)
    					clock.value = maxSeconds - counts;
    			function onCountDown() {
    				if (++counts >= maxSeconds) {
                    	location.href = "xxxxxx.asp";
    			// Start count down once page has loaded.
    			window.onload = function() {
    				countDownID = setInterval(onCountDown, 1000);
    		<h1>Demo Page</h1>
    			<input type="text" id="clock">
    			<input type="button" value="Cancel Count Down" onclick="return stopCountDown()">

    Open in new window


    Author Closing Comment


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now