2013-10-01 25 views
0

我正在嘗試編寫一個函數,它根據時間由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 

}); 

我能想到這樣做的最好的方法是通過對開始時間的產值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/

+0

這看起來像一個學校的問題? –

+0

不,這不是一個家庭作業問題@infensus – Liam

+0

肯定不會@MilchePatern – Liam

回答

1

像這樣的事情?

var currentDate = new Date(); 

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

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