webdevRefinery Forum: What is the problem? - webdevRefinery Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Rate Topic: -----

User is offline Sole_Wolf 

  • Group: Members
  • Posts: 303
  • Joined: 24-March 10
  • Expertise:HTML,PHP,Java,SQL

Posted 22 June 2012 - 04:48 PM (#1)

What is the problem?


Here is the code that I have and I have labeled different points of the code to show what the value of characters is.
function text(text) {
    characters = ['a','b','c','d','e','f'];
    // POINT 1 outputs correctly
    textdone='';
    
    for(i=0;i<characters.length;i++) {
        textdone = $('#battletxt').text();
        character=characters[i];
        // POINT 2 outputs correctly
        setTimeout(
            function(textdone,character) {
                alert(character);
                // POINT 3 outputs all NULL
                $('#battletxt').text(character);
            },100);
    }		
}


At point 3 for some reason, the code alerts a message that says undefined.

Does anyone know what is causing this problem or how to fix it?

Thanks.
--Sole_Wolf
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 22 June 2012 - 05:28 PM (#2)

character
is an argument of the function, and so will be redeclared (in this case, to undefined).
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 22 June 2012 - 05:29 PM (#3)

View PostSole_Wolf, on 22 June 2012 - 04:48 PM, said:

Here is the code that I have and I have labeled different points of the code to show what the value of characters is.
function text(text) {
	characters = ['a','b','c','d','e','f'];
        // POINT 1 outputs correctly
	textdone='';
	
	for(i=0;i<characters.length;i++) {
		textdone = $('#battletxt').text();
		character=characters[i];
                // POINT 2 outputs correctly
		setTimeout(
			function(textdone,character) {
				alert(character);
                                // POINT 3 outputs all NULL
				$('#battletxt').text(character);
			},100);
	}		
}


At point 3 for some reason, the code alerts a message that says undefined.

Does anyone know what is causing this problem or how to fix it?

Thanks.


function text(text) {
	characters = ['a','b','c','d','e','f'];
        // POINT 1 outputs correctly
	textdone='';
	
	for(i=0;i<characters.length;i++) {
		textdone = $('#battletxt').text();
		character=characters[i];
                // POINT 2 outputs correctly
		setTimeout(
			function() {
				alert(character);
                                // POINT 3 outputs all NULL
				$('#battletxt').text(character);
			},100);
	}		
}

Reserved.
0


User is online callumacrae 

  • {{ post.author }}
  • Group: Members
  • Posts: 2862
  • Joined: 20-January 11
  • LocationWarwickshire, England
  • Expertise:HTML,CSS,PHP,Javascript,Node.js,SQL

Posted 22 June 2012 - 05:30 PM (#4)

View PostThatRailsGuy, on 22 June 2012 - 05:29 PM, said:

function text(text) {
	characters = ['a','b','c','d','e','f'];
        // POINT 1 outputs correctly
	textdone='';
	
	for(i=0;i<characters.length;i++) {
		textdone = $('#battletxt').text();
		character=characters[i];
                // POINT 2 outputs correctly
		setTimeout(
			function() {
				alert(character);
                                // POINT 3 outputs all NULL
				$('#battletxt').text(character);
			},100);
	}		
}


You forgot to fix the awful formatting!
Front-end developer and writer
Twitter | GitHub | phpBB Contributor and Website Team Member | lynxphp
0


User is offline arronhunt 

  • I'm a httpster
  • Group: Moderators
  • Posts: 3398
  • Joined: 09-March 10
  • LocationLos Angeles, CA
  • Expertise:HTML,CSS,Javascript,Graphics,Flash

Posted 22 June 2012 - 05:59 PM (#5)

Also
textDone
for consistant camelCase formatting
DO NOT OPEN THIS

Spoiler
0


User is offline lobabob 

  • Group: Members
  • Posts: 98
  • Joined: 21-March 10
  • LocationWouldn't you like to know?
  • Expertise:HTML,CSS

Posted 22 June 2012 - 10:42 PM (#6)

Doesn't the above just repeat the character 'f' six times instead of going through the entire array?

why don't you just try something along the lines of this:

function text(text) {
        characters = text.split('');
        textDone='';
		i=0;
		
		disloop = setInterval(function() {
			textDone = $('#battletxt').text();
			$('#battletxt').text(textDone+characters[i]);
			
			i++;
			if(i==characters.length) {
				clearInterval(disloop);
			}
		},30);
}



ignore formatting please :(
"When the world goes mad, one must accept madness as sanity since sanity is, in the last analysis, nothing but the madness on which the whole world happens to agree." - George Bernard Shaw

Life = http://xkcd.com
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 22 June 2012 - 11:06 PM (#7)

View Postlobabob, on 22 June 2012 - 10:42 PM, said:

Doesn't the above just repeat the character 'f' six times instead of going through the entire array?

no, because it is inside of a for loop.
Reserved.
0


User is offline lobabob 

  • Group: Members
  • Posts: 98
  • Joined: 21-March 10
  • LocationWouldn't you like to know?
  • Expertise:HTML,CSS

Posted 23 June 2012 - 12:47 PM (#8)

It repeated when I ran it though......
"When the world goes mad, one must accept madness as sanity since sanity is, in the last analysis, nothing but the madness on which the whole world happens to agree." - George Bernard Shaw

Life = http://xkcd.com
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 23 June 2012 - 01:22 PM (#9)

Did you copy and paste? I'll have to take a look at it later when I'm on a computer.
Reserved.
0


User is offline TheEmpty 

  • I say words in sequences.
  • Group: Members
  • Posts: 5154
  • Joined: 02-October 10
  • Expertise:HTML,CSS,PHP,Java,Javascript,Python,Ruby on Rails,SQL

Posted 23 June 2012 - 04:11 PM (#10)

Yeah, it's because of the timeout, strange. This worked though
function text(text) {
        characters = ['a','b','c','d','e','f'];
        // POINT 1 outputs correctly
        textdone='';
        
        for(i=0;i<characters.length;i++) {
                character=characters[i];
                // POINT 2 outputs correctly
                console.log(character)
        }               
}

Reserved.
0


User is offline Qasim 

  • Group: Members
  • Posts: 520
  • Joined: 25-October 10
  • LocationOntario, Canada
  • Expertise:HTML,CSS,PHP,Java,Javascript,Graphics

Posted 23 June 2012 - 04:29 PM (#11)

Yes, the timeout calls does the 100ms delay, however the For loop is done within 100ms making i equal "f", so therefore it alerts "f" every time when the timeout is complete. This happens because javascript is synchronous, meaning everything is ran at the same time.

What you need to look into to solve this problem is asynchronous looping, here is a good stackoverflow question on it:
http://stackoverflow...e-in-javascript
Qasim Iqbal
[HTML] [CSS] [JS] [PHP] [JAVA]
[ANDROID DEVELOPER] [CHROME WEB STORE DEVELOPER]
[WEBSITE] [FACEBOOK] [FORRST]
1


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users


Enter your sign in name and password


Sign in options
  Or sign in with these services