0

我正在尝试编写一个函数,根据时间,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

});

我能想到的最好方法是if对 startTime 输出的值进行声明......

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/

4

1 回答 1

1

像这样的东西?

var currentDate = new Date(); 

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

//subtract off your start time
 $('#call-stats').text((hour - startTime) * callInterval);
于 2013-10-01T14:48:40.590 回答