1

JavaScriptを使用してカウントダウンを作成しようとしています。ここからいくつかのコードを取得し、少し修正しました。

<script type="text/javascript">
var c=10, t;

function timedCount() {
  document.getElementById('txt').value=c;
  c=c-1;
  t=setInterval("timedCount()",1000);
}

function stopCount() {
  clearInterval(t);
}
</script>

ユーザーがリンクをクリックするまで、カウントダウンを繰り返し呼び出す必要があります。リンクがクリックされるまで、毎秒 10 から 1 ずつ (10,9,8,7,6...0) カウントダウンする必要がありますが、クリックされません。誰でも私を助けることができますか?

編集:カウントダウンが0になったらカウントダウンを再開する方法を知っている人はいますか?

前もって感謝します。

4

1 に答える 1

6
<script type="text/javascript">
var c=10;
var t;
function timedCount()
{
    document.getElementById('txt').value=c;
    c=c-1;
}
function startCount()
{
    if (!t) t=setInterval("timedCount()",1000);
}
function stopCount()
{
    clearInterval(t);
    t=null;
}
</script>

Call startCount() in onload (or whatever) when you want the counter started. Note my startCount and stopCount don't create multiple interval timers.

Also, the element with id=txt needs to be an <input> or <textarea> box for your code to work. If it's a span, you should use document.getElementById('txt').innerHTML=c;

Finally, you might want timedCount() to stopCount() if c goes below zero. This is easy enough:

if (c <= 0) stopCount();
于 2008-11-23T00:10:01.127 に答える