を勉強したくなるでしょうsetInterval()
。
コードは次のようになります。
var counter = 1,
lastUpdate = (new Date()).getTime(),
img = document.getElementById('image'); // Assuming your HTML has an img tag
// with an id of "image"
// This function just pads your number with 0s
function pad(num) {
var padding = '',
i = 4 - num.toString().length;
while (i > 0) {
padding += '0';
i -= 1;
}
return padding + num;
}
// This function is what actually does the updating
function update() {
var now = (new Date()).getTime();
if (lastUpdate + 1000 <= now) {
lastUpdate = now;
img.src = pad(counter) + '.jpg'; // change the image
counter += 1; // increment the counter
if (counter > 1440) { // reset to 1 if we get to our last image
counter = 1;
}
}
}
// Run update every 10th of a second
setInterval(update, 100);
Mozilla Developer Center サイトには、すばらしいJavaScriptとDOMのリファレンスがたくさんあります。また、 JSLintの使い方を学ぶことをお勧めします。これは、頭痛の種となる愚かな構文エラーを回避するのに大いに役立ちます。Douglas Crockford の本JavaSript: The Good Partsと Stoyan Stefanov のObject-Oriented JavaScriptを読むことをお勧めします。どちらも JavaScript を学ぶのに優れた本です。