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: 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: RE: Whats wrong with that var declaration? by markee on 08-17-2006 at 01:24 PM
code: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. RE: Whats wrong with that var declaration? by RaceProUK on 08-17-2006 at 02:24 PM
code: Edit: Don't hate me because I streamline code RE: Whats wrong with that var declaration? by markee on 08-17-2006 at 02:32 PM
quote: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: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: RE: Whats wrong with that var declaration? by RaceProUK on 08-17-2006 at 08:48 PM
quote: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 RE: RE: Whats wrong with that var declaration? by CookieRevised on 08-18-2006 at 12:00 AM
quote: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: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:(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: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: 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 |