jQuery variable increment ++ not working in script

I have this jQuery function that switches between different status messages (p elements) with ID's ranging from status_0 to status_5:

setTimeout(function() {
    var next_status;

    if (current_status < 5) {
        next_status = current_status++;
    } else {
        next_status = 0;
    }

    $(".status_visible").fadeOut("fast", function() {
        $("#status_"+next_status).fadeIn("fast");
    });

    //alert(next_status);

    change_status();
}, 10000);

My problem is that to start with, current_status definitely equals 0, but when it gets to my increment part, it comes out as 0 still! I tried this with a simple next_status = current_status + 1, which returned 01 instead of 1 (concatenated them), so I tried next_status = current_status++ and it returned 0 still.

Can anybody put me straight here please :)

Answers


next_status = ++ current_status;

The expression current_status++ means, "increment current_status, and evaluate to the old value of current_status," that is, use current_status, and then increment it after you note its value.

You want, ++current_status, which means, "increment current_status, and evaluate to the new value."


is your timeout method "setTimeout" going to be called multiple times? If so , you need to pass your counter as a parameter with global scope, otherwise it will be reset each time.


Need Your Help

IE 6 does not recognize the current page

css internet-explorer internet-explorer-6 navigation

I am using a simple list as navigation which brings up the appropriate background image depending on the state of the link normal, hover or current. In IE6 the current or actual page is being ignor...

Rewrite url issue

.htaccess coldfusion jrun

I'm hoping someone can help me. For my website I have a corresponding mobile site that has the same content as my full site but display it for mobile devices. Basically I want to send all requests...