tinfanide Posted October 20, 2011 Share Posted October 20, 2011 var i = -1;var arr;function wordScroller(){clearTimeout(display);arr = str.split(" ");i+=1;document.getElementById("div").innerHTML += str.substr(str.indexOf(arr[i]),arr[i].length) + " ";if(i<arr.length){ var display = setTimeout(wordScroller,1000) } else { clearTimeout(display); }} <!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=utf-8" /><title>JS: Word Scroller</title><script type="text/javascript" src="wordScroller.js"></script><script>window.onload = function(){str = "These are pretty girls";wordScroller();}</script></head><body><div id="div"></div></body></html> The word scroller works fine in both IE9 and FF5 but one problem is that FF Console reports after the animation finishes that arr is undefined.How could I possibly fix it? I have no idea coz I bet I ain't very sure about the "undefined" issue. Link to comment Share on other sites More sharing options...
Don E Posted October 20, 2011 Share Posted October 20, 2011 Since arrays begin at zero, you have to minus one in your if statement: if(i<arr.length-1){ var display = setTimeout(wordScroller,1000) } else { clearTimeout(display); } Should work. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 20, 2011 Share Posted October 20, 2011 You're not checking if arr exists before trying to use it when you're updating the innerHTML. Link to comment Share on other sites More sharing options...
tinfanide Posted October 21, 2011 Author Share Posted October 21, 2011 But it still works.By the way, how can I check if arr exists?Is that: if(arr[i]) Link to comment Share on other sites More sharing options...
justsomeguy Posted October 21, 2011 Share Posted October 21, 2011 It works until it gets to the end, and then... FF Console reports after the animation finishes that arr is undefined. You're already checking if i is less than arr.length, but you have a statement trying to use arr before you check that. Link to comment Share on other sites More sharing options...
tinfanide Posted October 21, 2011 Author Share Posted October 21, 2011 yes, but how to fix it? Link to comment Share on other sites More sharing options...
thescientist Posted October 21, 2011 Share Posted October 21, 2011 JSG just told you. You're already checking if i is less than arr.length, but you have a statement trying to use arr before you check that. put it inside the if statement too Link to comment Share on other sites More sharing options...
tinfanide Posted October 21, 2011 Author Share Posted October 21, 2011 Yes, I see the point of the if statement. var i = -1;var arr;function wordScroller(){clearTimeout(display);arr = str.split(" ");i+=1;if(i<arr.length){ document.getElementById("div").innerHTML += str.substr(str.indexOf(arr[i]),arr[i].length) + " "; var display = setTimeout(wordScroller,1000) } else { clearTimeout(display); }} Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.