Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Status bar animation confusion 1

Status
Not open for further replies.

zngo

Technical User
May 30, 2003
21
0
0
US
Hello, I have never used javascript seriously until now, since i got my computer (my webserver) taken away and i have been forced to use the webspace provided by my isp, where php (my fav lang) isn't available to me. Hence, Javascript! So, I wanted to make an animated status bar message but I also don't want to use someone else's source and use my own instead. I thought I knew how to do it but there is a strange error. here is the code:

------------------------------------------------------
<script language=&quot;Javascript&quot;>
var message=&quot;welcome to foo.com!&quot;;
var i=0;
function main(){
for (i=0;i<=message.length;i++){
setTimeout(&quot;window.status=message.substr(0,i)&quot;,500);
}
}
</script>
------------------------------------------------------

I did by the way, use the onload=&quot;main()&quot; tag in the html's body tag to fire up main(). When the page loads, it waits the half second that it is supposed to wait, but instead of printing the welcome message letter by letter, it prints the whole thing at once!

Help would be greeattly appreciated, I have spent so much time going crazy about this..
 
Try this code. Place it between your header tags.

<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
function statusMsg_makeArray(n){
this.length = n;
return this.length;
}

statusMsg_messages = new statusMsg_makeArray(1);
statusMsg_messages[0] = &quot;welcome to foo.com!&quot;;
statusMsg_rptType = 'finite';
statusMsg_rptNbr = 2;
statusMsg_speed = 100;
statusMsg_delay = 2000;
var statusMsg_counter=1;
var statusMsg_currMsg=0;
var statusMsg_tekst =&quot;&quot;;
var statusMsg_i=0;
var statusMsg_TID = null;
function statusMsg_pisi(){
statusMsg_tekst = statusMsg_tekst + statusMsg_messages[statusMsg_currMsg].substring(statusMsg_i, statusMsg_i+1);
window.status = statusMsg_tekst;
statusMsg_sp=statusMsg_speed;
statusMsg_i++;
if (statusMsg_i==statusMsg_messages[statusMsg_currMsg].length){
statusMsg_currMsg++; statusMsg_i=0; statusMsg_tekst=&quot;&quot;;statusMsg_sp=statusMsg_delay;
}
if (statusMsg_currMsg == statusMsg_messages.length){
if ((statusMsg_rptType == 'finite') && (statusMsg_counter==statusMsg_rptNbr)){
clearTimeout(statusMsg_TID);
return;
}
statusMsg_counter++;
statusMsg_currMsg = 0;
}
statusMsg_TID = setTimeout(&quot;statusMsg_pisi()&quot;, statusMsg_sp);
}
onload=statusMsg_pisi
-->
</script>
 
whew, that seems a little too much for me to understand right now, even though I am trying. In my script, I was trying to get it to perform just one successful loop, and printing the welcome message to the status bar only once. Is there possibly a slight mechanical error in my code or is it neccessary to just redo the whole thing?

-zach
 
Here is a script I found in javascriptkit homesite:

Code:
<script language=&quot;JavaScript&quot;>
<!--

/*
Written by BengalBoy Visit [URL unfurl="true"]http://javascriptkit.com[/URL]
*/

//set message:
msg = &quot;Welcome to JavaScript Kit!&quot;;

timeID = 10;
stcnt = 16;
wmsg = new Array(33);
        wmsg[0]=msg;
        blnk  &quot;&quot;;
        for (i=1; i<32; i++)
        {
                b = blnk.substring(0,i);
                wmsg[i]=&quot;&quot;;
                for (j=0; j<msg.length; j++) wmsg[i]=wmsg[i]+msg.charAt(j)+b;
        }

function wiper()
{
        if (stcnt > -1) str = wmsg[stcnt]; else str = wmsg[0];
        if (stcnt-- < -40) stcnt=31;
        status = str;
        clearTimeout(timeID);
        timeID = setTimeout(&quot;wiper()&quot;,100);
}

wiper()
// -->
</script>

I myself has one question, tho...How can I make the message written in [red]red[/red]???

-Tivoli0
 
zngo,

the message is &quot;appearing&quot; all at once because essentially your script executes the calls to setTimeout() at full speed, so you end up with 19 calls almost simultaneously, which looks like everything at once.

you should make your function main() recursive:

<script language=&quot;Javascript&quot;>
var message=&quot;welcome to foo.com!&quot;;
var i=0;

function main(){
if (i++ <= message.length) {
window.status=message.substr(0,i);
setTimeout(&quot;main();&quot;,500);
}
}
</script>

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Jeff,

While everyone tried to help me, you actually told me why my code wasnt working. Thanks for your help.

-Zach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top