SetTimeout doesnt work in for loop

SetTimeout most unusable function in jscript. much internet searching, much confusion, this function not work like people expect, cause much problem, like only working answer with code please.

easy for loop --

var msg = 'here is msg many lines long';
var strlen=msg.length;
var msgstr="";
var i=0;
var char= "";
for (i=0; i<strlen; i++)
{   char = charAt(i);
     msgstr= msgstr + char;
     setTimeout("parent.document.getElementById(tfield).innerHTML=msgstr", 300);
 }

This never work.  always get error msgstr not defined, or any other var not defined.
all varibales properly defined.  problem is with settimeout, very badly thouught out.
no intersted why, just need way to make settimeout work in for loop to delay add of char to string.
cant get it to work like help or ideas to give code to make work, thank you
LVL 44
scrathcyboyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WoodyRoundUpCommented:
What happen if you try this:

setTimeout("parent.document.getElementById(tfield).innerHTML='"+msgstr+"'", 300);
scrathcyboyAuthor Commented:
tried, no work either, quoting not problem, problem is setTimeout kill variables in loop, next call variables all gone -- you pls look into setTimeout, you see it is weird function not acting like normal jscript, wont work in for loop, is not right?
WoodyRoundUpCommented:
<script type="text/javascript">
var msg = "here is msg many lines long";
var strlen=msg.length;
var msgstr="";
var i=0;
var mychar= "";

for (i=0; i<strlen; i++)
{  
     mychar = msg.substr(i,1)
     msgstr= msgstr + mychar;
    setTimeout("parent.document.getElementById(tfield).innerHTML='"+msgstr+"'", 300);
 }
 </script>

this one should works fine.
You got 3 errors in your script, when I tried on my machine.
var char
i believe char is the reserved variables for javascript.

and charAt for some reason is not working. That's why I replace it with substr. Probably it's only for IE. Maybe some experts here can help out to explain why.

And the third one it's because the msgstr is passed in as a variable, and it will be displayed straightaway without delaying (as tested with alert() on my machine. But if I replace the string in the setTimeOut, it works fine.

Hope this helsp.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

WoodyRoundUpCommented:
Ok. I found out why charAt does not work.
Lol.

It does not work like this:
var msg="abc";

var m = charAt(i) (Your Original Code);
var n = charAt (msg,i) - I tested this, not working.

It is supposed to be like the substr in my sample code:
which is:

var o = msg.charAt(i);

then this will return result.
iamanindianCommented:
Assuming....tfield is a textfiled...try running your code like:


parent.document.getElementById(tfield).value         //<--innerHTML replaced by value


Perhaps this will work.
Regards
WC
scrathcyboyAuthor Commented:
sorry for typos, need to have quote on tfield   .getElementById('tfield') and '"+msgstr+"'" to get msg in parent.  but still not working .. Trying to delay each character of message by setTimeout value 300, but doesnt work.  SetTimeout() only causes delay in whole message coming on screen, not delay for each character.  Want characters to show one by one, delay between, settimeout in 'for' loop doesnt work.
iamanindianCommented:
Well...what exactly are you looking for? Is it a sort of scrolling text? Or...you want your "msg" to appear character by characte in your defined text field?

Are you still getting errors? What did you mean by "but still not working"?

Regards
WC
WoodyRoundUpCommented:
The reason why this will display all the text at one go is because your setTimeOut was set to 300.
When you call the setTimeOut, the function in the setTimeOut will be called after 300 miliseconds (or 0.3 second).
While your loop is still running. By the time the 300 miliseconds is finished, your while loop is already finish long time ago. Therefore, the text will be displayed with the last variable passed in, which is the whole string.

To make this work, after you setTimeOut ("your statement",300), you will need to add a time delay so the loop won't be started after the 300 miliseconds has passed.
This post might help you for the Time Delay.

http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20088757.html

Hope this helps.
scrathcyboyAuthor Commented:
Said I wanted msg to appear character by character.  Need fix for code as asked in question -

"just need way to make settimeout work in for loop to delay add of char to string.
cant get it to work like help or ideas to give code to make work"

Asked for fixed code for character by character display 300 ms apart, is it not clear?  
scrathcyboyAuthor Commented:
Solved problem on own.  As thought, setTimeout doesnt work in 'for' loop
spent much time changing function many way, killed for loop, then function work.
thank you for help, but answer I think is setTimeout doesn't work in for loop.
WoodyRoundUpCommented:
I have mentioned to you the reason why it does not work.

"The reason why this will display all the text at one go is because your setTimeOut was set to 300.
When you call the setTimeOut, the function in the setTimeOut will be called after 300 miliseconds (or 0.3 second).
While your loop is still running. By the time the 300 miliseconds is finished, your while loop is already finish long time ago. Therefore, the text will be displayed with the last variable passed in, which is the whole string."

And your question was asking for the idea, and I have given you one.

"To make this work, after you setTimeOut ("your statement",300), you will need to add a time delay so the loop won't be started after the 300 miliseconds has passed.
This post might help you for the Time Delay."

Glad that you solved the problem.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.