使用jQuery以小时为间隔为现有值添加值

时间:2013-10-01 14:22:17

标签: javascript jquery

我正在尝试编写一个函数,根据时间,jQuery生成一个值,并且每小时都会使用新值更新它,直到达到最大值(变量集)。

$(document).ready(function(){

    function randomGenerator(bottom, top) {
        return Math.floor( Math.random() * ( 1 + top - bottom ) ) + bottom;
    }

    var startTime = $('.start-time').val();
    var callLimit = randomGenerator( 120, 180 );
    var callInterval = callLimit/12;

    $('#call-stats').text(
        // at 7am value will equal '0'
        // at 9am value will equal 'callLimit * 2' as 2 hours have passed
        // at 3pm value will equal 'callLimit * 8' as 8 hours have passed
    );

    // Value cannot go above that set in 'callLimit' and the function will reset everyday

});

我能想到这样做的最好方法是通过startTime对输出值进行if语句...

if( startTime === '07:00' ) {
    $('#call-stats').text('0');
} 
else if( startTime === '08:00' ) {
    $('#call-stats').text(callInterval);
} 
else if( startTime === '09:00' ) {
    $('#call-stats').text(callInterval * 2);
} 
else if( startTime === '10:00' ) {
    $('#call-stats').text(callInterval * 3);
} 
...

有人有更有效的方法吗?

http://jsfiddle.net/Zz2RY/1/

1 个答案:

答案 0 :(得分:1)

这样的东西?

var currentDate = new Date(); 

//Clock is 24 hour
var hour = currentDate.getHours();

//subtract off your start time
 $('#call-stats').text((hour - startTime) * callInterval);