55502f40dc8b7c769880b10874abc9d0

This js will take a value, subtract X amount from it, and then count up to that value. It seems like I should be able to do this with a simple While loop, but I haven't been able to get it to work.

This is the only way it will work.

function _initCountUpUsers()
{
    var totalUsers = $('#totalUsers');
    var cieling = parseInt(totalUsers.html());
    var floor = cieling - 10;

    function updateTotalUsers()
    {
        if(floor < cieling)
        {
            totalUsers.html(floor);
            floor++;
        }
    }

    window.setInterval(updateTotalUsers, 1000, floor);
}

Refactorings

No refactoring yet !

Aacfa176a8d73ca75b90b6375151765a

paul.wilkins.myopenid.com

August 3, 2010, August 03, 2010 03:18, permalink

No rating. Login to rate!

You can use clearInterval from within the update function to step the process when the time is right.

You may also want to use the correct spelling for ceiling.

This code counts from 0 to totalUsers-1 as does your original code.
If you want it to instead count from 1 to totalUsers, move the if condition block to the start of the function.

function _initCountUpUsers()
{
    var totalUsers, ceiling, floot, update;

    totalUsers = $('#totalUsers');
    ceiling = parseInt(totalUsers.html());
    floor = ceiling - 10;
	
    function updateTotalUsers()
    {
        totalUsers.html(floor);
        floor++;
        if (floor === ceiling) {
            clearInterval(update);
        }
    }
    update = window.setInterval(updateTotalUsers, 1000, floor);
}

Your refactoring





Format Copy from initial code

or Cancel