
//-----------------------------------------------------
//  To reduce counter consuming CPU time:
//  if incrementAmountPerTimeInterval is more than 1, 
//  timeInterval should be set to 1.
//------------------------------------------------------

function counter(counterNum, incrementAmountPerTimeInterval, numberOfDigits, containerId, timeInterval)
{
    if(1 < incrementAmountPerTimeInterval)
    { 
        counterNum += incrementAmountPerTimeInterval;
    }
    else
    {
        counterNum++;
    }
    
    strCounterNum = String(counterNum);

    var counterDigitLength = strCounterNum.length;

    if(numberOfDigits > counterDigitLength)
    {
        for(var i = counterDigitLength; i < numberOfDigits; i++)
        {
            strCounterNum = 0 + strCounterNum;
        }
    }

    var html = '';
    for(var i = 0; i < numberOfDigits; i++)
    {
        html += '<div>'+strCounterNum.charAt(i)+'</div>';
    }


    $("#"+containerId).html(html);

    window.setTimeout(function(){
        counter(counterNum, incrementAmountPerTimeInterval, numberOfDigits, containerId, timeInterval)
    }, timeInterval);
}
