setTimeout won't work

This is a discussion on "setTimeout won't work" within the JavaScript Forum section. This forum, and the thread "setTimeout won't work are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Dec 24th, 2006, 18:28
Junior Member
Join Date: Dec 2006
Location: nantwich
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
setTimeout won't work

hey all,

in a piece of code im working on i need the function to be executed every second so i used this code:



Code: Select all
function timer(){
	var today = new Date();
	var timeNow = today.getTime();
	
	var target = new Date("23 July, 2007");
	var timeTarget = target.getTime();
	
	seconds = Math.floor((timeTarget - timeNow) / 1000);
	secs = String(seconds % 60);

	minutes = Math.floor(seconds / 60);
	mins = String(minutes % 60);

	hours = Math.floor(minutes / 60);
	hrs = String(hours % 24);

	days = Math.floor(hours / 24);
	days = String(days);
	timeLeft = days + " days "+hrs+" hours "+mins+" minutes and "+secs+" seconds left";
	document.write(timeLeft);
	ID=window.setTimeout("timer()",1000);
}
the problem is that the timeout won't work and i get an error message everytime.
im not sure why this is because i have looked it up and this seems to be the right code.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Dec 27th, 2006, 00:26
JUD JUD is offline
New Member
Join Date: Jul 2006
Location: I'm right here
Age: 30
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: setTimeout won't work

First of all, you won't want to use document.write(timeLeft) as this will keep adding to the string instead of updating it.

Instead put this function in the head of the document and update the string using the DOM (e.g document.getElementById('countdown').firstChild.da ta = timeLeft)

There's also no need to assign your setTimeout to a variable. Just call it at the end of your function.

Here's a complete script that works (Tested in FF 2.0 and IE7).

HTML: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
    // <![CDATA[
        function timer(){
            var today = new Date();
            var timeNow = today.getTime();
            
            var target = new Date("23 July, 2007");
            var timeTarget = target.getTime();
            
            seconds = Math.floor((timeTarget - timeNow) / 1000);
            secs = String(seconds % 60);
        
            minutes = Math.floor(seconds / 60);
            mins = String(minutes % 60);
        
            hours = Math.floor(minutes / 60);
            hrs = String(hours % 24);
        
            days = Math.floor(hours / 24);
            days = String(days);
            timeLeft = days + " days "+hrs+" hours "+mins+" minutes and "+secs+" seconds left";
            document.getElementById('countdown').firstChild.data = timeLeft;
            setTimeout("timer()",1000);
        }
    // ]]>
</script>
</head>

<body onload="timer()">
<span id="countdown">&nbsp;</span>
</body>
</html>

Last edited by JUD; Dec 27th, 2006 at 00:34.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
settimeout problem

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with setTimeout in method Emancip8 JavaScript Forum 1 Apr 22nd, 2008 16:30
Javascript menu - does not work on title page, but does work on other pages Crystal Phoenix Starting Out 4 Mar 24th, 2008 19:40
.call or .apply with setTimeout djeyewater JavaScript Forum 5 Feb 29th, 2008 16:41
simple js doesn't work with IE7 (work with FF1.5) gpazi JavaScript Forum 1 Jun 10th, 2007 11:00
"setTimeout" function won't work QuikFrozen JavaScript Forum 2 Aug 7th, 2006 16:52


All times are GMT. The time now is 00:32.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved