SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    script from js anthology 101 from sitepoint

    The following script is from the js anothology 101 from sitepoint. its the js clock script on page 182.

    I wanted to implement this script. I'm using the following html and js but I keep getting an error that digitImages[time[i]].src (on line 32) has no properties.. unless i missed some code I don't understand why its not working.

    HTML Code:
    <div>
    	<p>Current Time:</p>
    		<img id="d0" src="images/0.gif" alt="0">
    		<img id="d1" src="images/0.gif" alt="0">
    		<img src="images/colon.gif" alt=":">
    		<img id="d2" src="images/0.gif" alt="0">
    		<img id="d3" src="images/0.gif" alt="0">
    		<img src="images/colon.gif" alt=":">
    		<img id="d4" src="images/0.gif" alt="0">
    		<img id="d5" src="images/0.gif" alt="0">
    </div>
    Code:
    window.onload = initAll;
    
    var digitImages = [];      //caches image gifs 0 - 9
    
    		for(var i = 0; i < 10; i++){
    			digitImages[i] = new Image();
    			digitImages[i].src = "images/" + i + ".gif";
    		}
    
    function setTime(){
    	
    	//this clock works on a 6 digit time.
    	
    	var now = new Date();
    	var time = [];
    	
    	var hrs = now.getHours();
    	hrs = (hrs < 10 ? '0' : '') + hrs;
    	time[0] = hrs.charAt[0];
    	time[1] = hrs.charAt[1];
    	
    	var min = now.getMinutes();
    	min = (min < 10 ? '0' : '') + min;
    	time[2] = min.charAt[0];
    	time[3] = min.charAt[1];
    	
    	var sec = now.getSeconds();
    	sec = (sec < 10 ? '0' : '') + sec;
    	time[4] = sec.charAt[0];
    	time[5] = sec.charAt[1];
    	
    	for(var i = 0; i < time.length; i++){
    		var digit = document.getElementById('d' + i);
    		digit.src = digitImages[time[i]].src;   //error on this line?
    		digit.alt = time[i];
    	}
    }
    
    function initAll(){
    	setTime();
    	setInterval("setTime()", 1000);
    }

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    You're using [] instead of () for charAt
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Guru tictike's Avatar
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    863
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect! Thanks.


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
  •