Jump to content

Need Help On "undefined"


tinfanide

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...