What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Whats wrong with that var declaration?

Whats wrong with that var declaration?
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: RE: Whats wrong with that var declaration?
quote:
Originally posted by SmashD
I am unable to figure out why the x_img variable isn't correct... :(
Although people have shown how the code should be. I don't  think anybody explained actually why:

The reason why the x_img variable wasn't correct is because in your first script (top post) it was not declared.

When a variable is not declared, JSCript declares it for you as soon as it encounters it. In your case, the first time JScript encounters the variable is inside the function itself.

Hence, it will be declared as a local variable.

Local variables do not maintain their value outside the function in where they were declared, in fact they wont even exist outside the function.

So each time you call the function, the variable is re-initialized and thus always reset to 0.

What you wanted requires that the variable maintains its value once the function has ended, so it could be used again with that current value the next time the function is called.

To do this you needed to declare the variable as a global variable, thus outside of the function. In that way the variable will be valid thruout your entire script (thus also in other functions for that matter) and will thus maintain its value.




------------------------------------------------------------------------------------------




quote:
Originally posted by hmaster
Oh! didn't know you could use it like that (Y)
The braces ({}) are used to group a set of statements into one block. Statements grouped into a block can generally be treated as a single statement. This means you can use blocks in most places that JScript expects a lone statement like in IF THEN ELSE structures. Thus if you only have 1 statement you don't need to use them in most cases. eg:
code:
if (blah === "banana")
     HelloWorld = true
else
     HelloWorld = false;
(but note that you must use them to define a function, even if that function only consists of 1 line)

PS: but be carefull in not using the braces; it is a common mistake to not use them in the wrong places and thus is prone to mistakes/bugs (especially when you nest multiple IF THEN ELSE structures and what not). Better be safe than 2-hours-searching-after-that-bug-sorry.


------------------------------------------------------------------------------------------





quote:
Originally posted by RaceProUK
code:
var x_img = 1; (edit by cookie: for the others, just put 0 here and you wont miss image1; its quite obvious (maybe it was even a test from RaceProUK against the copy/paste kiddies :p))

function OnEvent_Timer(sTimerId) {
    if (sTimerId == "slider" && Messenger.MyStatus == 3){
        x_img++;
        if (x_img == 4) {
            x_img = 0;
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online.jpg";
        }
        else Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online" + x_img + ".jpg";
        MsgPlus.AddTimer("slider", 20000);
    }
}
Don't hate me because I streamline code :P

don't hate me for optimizing it even more to only 3 lines :XP:
(also notice the use of identity comparison (===) which is also faster than the equality comparison (==))
code:
var x_img = 0;

function OnEvent_Timer(sTimerId) {
    if (sTimerId === "slider" && Messenger.MyStatus === 3) {
        x_img = ++x_img % 4;
        Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online" + x_img + ".jpg";
        MsgPlus.AddTimer("slider", 20000);
    }
}


Lookup the 'modulus' operator (%). If you ever want to cycle between numbers, the modulus operator is almost mandatory to use!

x_img = 0;
x_img = ++x_img % 4;      // => x_img = 1
x_img = ++x_img % 4;      // => x_img = 2
x_img = ++x_img % 4;      // => x_img = 3
x_img = ++x_img % 4;      // => x_img = 0
x_img = ++x_img % 4;      // => x_img = 1
x_img = ++x_img % 4;      // => x_img = 2
x_img = ++x_img % 4;      // => x_img = 3
x_img = ++x_img % 4;      // => x_img = 0
x_img = ++x_img % 4;      // => x_img = 1
etc...

the images needed for the above:
online0.jpg, online1.jpg, online2.jpg, online3.jpg


If you want to use these images (makes more sense imho):
online1.jpg, online2.jpg, online3.jpg, online4.jpg,

then change that modulus line to:
x_img = x_img % 4 + 1;

This post was edited on 08-18-2006 at 12:32 AM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
08-18-2006 12:00 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Whats wrong with that var declaration? - by SmashD on 08-17-2006 at 01:12 PM
RE: Whats wrong with that var declaration? - by mickael9 on 08-17-2006 at 01:18 PM
RE: Whats wrong with that var declaration? - by markee on 08-17-2006 at 01:24 PM
RE: Whats wrong with that var declaration? - by SmashD on 08-17-2006 at 01:28 PM
RE: Whats wrong with that var declaration? - by RaceProUK on 08-17-2006 at 02:24 PM
RE: Whats wrong with that var declaration? - by markee on 08-17-2006 at 02:32 PM
RE: Whats wrong with that var declaration? - by RaceProUK on 08-17-2006 at 08:38 PM
RE: Whats wrong with that var declaration? - by hmaster on 08-17-2006 at 08:44 PM
RE: Whats wrong with that var declaration? - by RaceProUK on 08-17-2006 at 08:48 PM
RE: Whats wrong with that var declaration? - by hmaster on 08-17-2006 at 09:02 PM
RE: RE: Whats wrong with that var declaration? - by CookieRevised on 08-18-2006 at 12:00 AM
RE: Whats wrong with that var declaration? - by Stigmata on 08-18-2006 at 12:13 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On