如何将此间隔从每 2 秒变为随机 X - X 秒?另外,如何将增量从每 2 个增量更改为随机 X - X?如果您发现我的代码中存在缺陷或需要改进,请同时告诉我,如果愿意,请在单独的代码中。非常感激。
<div id="counter"></div>
<script type="text/javascript">
var START_DATE = new Date("January 1, 2012 12:00:00"); // put in the starting date here
var INTERVAL = 2; // in seconds
var INCREMENT = 2; // increase per tick
var START_VALUE = 5000; // initial value when it's the start date
var count = 0;
window.onload = function()
{
var msInterval = INTERVAL * 2000;
var now = new Date();
count = parseInt((now - START_DATE)/msInterval) * INCREMENT + START_VALUE;
document.getElementById('counter').innerHTML = count;
setInterval(
"count += INCREMENT; document.getElementById('counter').innerHTML = count;", msInterval);
}
</script>