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
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 ))
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
don't hate me for optimizing it even more to only 3 lines
(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:
online
0.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;