• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Disable Javascript countdown function via click...

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?
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
var start=new Date();
	start=Date.parse(start)/1000;
	var counts=10;
	function CountDown(){
		var now=new Date();
		now=Date.parse(now)/1000;
		var x=parseInt(counts-(now-start),10);
		if(document.form1){document.form1.clock.value = x;}
		if(x>0){
			timerID=setTimeout("CountDown()", 100)
		}else{
			location.href="xxxxxx.asp"
		}
	}
//  End -->
</script>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.setTimeout('CountDown()',100);
-->
</script>
 
</HEAD>

Open in new window

0
Mitchel-T
Asked:
Mitchel-T
1 Solution
 
numberkruncherCommented:
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" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>Demo Page</title>
		<script type="text/javascript">
			var countDownID, counts = 0, maxSeconds = 10;
			function stopCountDown() {
				if (countDownID) {
					clearInterval(countDownID);
					countDownID = null;
				}
			}
			function updateClock() {
				var clock = document.getElementById('clock');
				if (clock)
					clock.value = maxSeconds - counts;
			}
			function onCountDown() {
				if (++counts >= maxSeconds) {
                	stopCountDown();
                	location.href = "xxxxxx.asp";
				}
				else
					updateClock();
			}
 
			// Start count down once page has loaded.
			window.onload = function() {
				countDownID = setInterval(onCountDown, 1000);
				updateClock();
			}
		</script>
	</head>
	<body>
		<h1>Demo Page</h1>
		<div>
			<input type="text" id="clock">
			<input type="button" value="Cancel Count Down" onclick="return stopCountDown()">
		</div>
	</body>
</html>

Open in new window

0
 
Mitchel-TAuthor Commented:
Perfect...thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now