Shoutbox

Whats wrong with that var declaration? - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: Whats wrong with that var declaration? (/showthread.php?tid=65040)

Whats wrong with that var declaration? by SmashD on 08-17-2006 at 01:12 PM

code:
function OnEvent_Timer(slider){
if (Messenger.MyStatus == 3){
var x_img = x_img + 1;
if(x_img == 1) {
Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online2.jpg";
}
else if(x_img == 2)
{
Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online3.jpg";
}
else if(x_img == 3)
{
Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online4.jpg";
}
else if(x_img == 4)
{
Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online.jpg";
var x_img = 0;
}
  MsgPlus.AddTimer("slider", 20000);
  return Message; 
}
}

It's triggered by an AddTimer - works nicely, but I am unable to figure out why the x_img variable isn't correct... :(
RE: Whats wrong with that var declaration? by mickael9 on 08-17-2006 at 01:18 PM

code:
var x_img = 0;

function OnEvent_Timer(sTimerId)
{
    if (sTimerId == "slider" && Messenger.MyStatus == 3)
    {
        x_img++;
       
        if(x_img == 1)
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online2.jpg";
        }
        else if(x_img == 2)
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online3.jpg";
        }
        else if(x_img == 3)
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online4.jpg";
        }
        else if(x_img == 4)
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online.jpg";
            x_img = 0;
        }
        MsgPlus.AddTimer("slider", 20000);
    }
}
:D
RE: Whats wrong with that var declaration? by markee on 08-17-2006 at 01:24 PM

code:
var x_img = 0; //set x_img as a global variable

function OnEvent_Timer(sTimerId) //sTimerId is a variable not what timer is being initiated
{
    if (sTimerId == "slider" && Messenger.MyStatus == 3)//make sure it is the right timer being initiated AND the status you wanted
    {
        x_img++;//add 1 to x_img
       
        if(x_img == 1)
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online2.jpg";
        }
        else if(x_img == 2)//used "else if" as this links if statements and only oe can be true
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online3.jpg";
        }
        else if(x_img == 3)
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online4.jpg";
        }
        else if(x_img == 4)
        {
            Messenger.MyDisplayPicture = MsgPlus.ScriptFilesPath + "\\images\\online.jpg";
            x_img = 0;
        }
        MsgPlus.AddTimer("slider", 20000);
    }
}
I added some note so you can understand the changes that have been made by mickael9.  I hope this gives you more understanding of what is happening.
RE: Whats wrong with that var declaration? by SmashD on 08-17-2006 at 01:28 PM

Oh, it's more like PHP than I guessed. Just fiddling around with that scripting since some minutes. Thanks, I'll try and report if that works.

EDIT: Works like a charm. Thanks alot.

EDIT2: Oh, one important thing. Once the timer event is fired the timer does not exist any longer, so I have to start a new one, right?
Of course I don't want to fill up my system with timers after some time. hehe


RE: Whats wrong with that var declaration? by RaceProUK on 08-17-2006 at 02:24 PM

code:
var x_img = 1;

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);
    }
}

Edit: Don't hate me because I streamline code :P
RE: Whats wrong with that var declaration? by markee on 08-17-2006 at 02:32 PM

quote:
Originally posted by RaceProUK
code:
var x_img = 1;

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);
    }
}

Edit: Don't hate me because I streamline code :P
That won't give what he was after, you will miss x_img = 1 at the begining.  Either make "var x_img = 0;" at the top or put "x_img++;" in the line after "MsgPlus.AddTimer("Slider",20000);" rather than where it is atm.
RE: Whats wrong with that var declaration? by RaceProUK on 08-17-2006 at 08:38 PM

quote:
Originally posted by markee
you will miss x_img = 1 at the begining
Oh boo-hoo. I only streamlined the code to show a few tricks.
RE: Whats wrong with that var declaration? by hmaster on 08-17-2006 at 08:44 PM

quote:
Originally posted by RaceProUK
code:
var x_img = 1;

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);
        }
    }
}
Edit: Don't hate me because I streamline code
Just to let you know if you are going to use that theres a missing '{'
RE: Whats wrong with that var declaration? by RaceProUK on 08-17-2006 at 08:48 PM

quote:
Originally posted by hmaster
Just to let you know if you are going to use that theres a missing '{'
I assure you there isn't: I checked. The brace you added will not only change how the script works, but also break it totally.

I use indenting to pair up braces. In fact, I usually write both the opening and closing braces befor I write the statements inside. That way I rarely drop a brace.

I drop parentheses a fair bit though.
RE: Whats wrong with that var declaration? by hmaster on 08-17-2006 at 09:02 PM

Oh! didn't know you could use it like that (Y)


RE: RE: Whats wrong with that var declaration? by CookieRevised on 08-18-2006 at 12:00 AM

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;
RE: Whats wrong with that var declaration? by Stigmata on 08-18-2006 at 12:13 AM

btw, in future.. if you are going to use alot of if else's etc use switch case

its much quicker