0

私を夢中にさせた小さな問題があります.mysqlデータベースからレコードを取得し、これらのレコードを表示しています.各レコードの下には、mysqlから値を取得するカウントダウンjavascriptクロックが必要です.最初のレコードでのみ機能し、他のレコードは text のみです。HTML 、PHP、および JS は次のとおりです。

<a>
    <div>
    <? echo $row['Device Name']; ?>
    </div>
    <? $Remain = $row['Remaining Time']; ?>
    <font color="red" size="6"><span id="countdown"> <? echo $Remain; ?></span></font>
    <img src="img/stop.png" /> 
    </a>  



<script type="text/javascript">

    // Initialize clock countdowns by using the total seconds in the elements tag

   secs       =  parseInt(document.getElementById('countdowno').innerHTML,10);
   setTimeout("countdown('countdowno',"+secs+")", 1000);




    /**
     * Countdown function
     * Clock count downs to 0:00 then hides the element holding the clock
     * @param id Element ID of clock placeholder
     * @param timer Total seconds to display clock
     */
    function countdown(id, timer){
        timer--;
        minRemain  = Math.floor(timer / 60);
        secsRemain = new String(timer - (minRemain * 60));
        // Pad the string with leading 0 if less than 2 chars long
        if (secsRemain.length < 2) {
            secsRemain = '0' + secsRemain;
        }

        // String format the remaining time
        clock      = minRemain + ":" + secsRemain;
        document.getElementById(id).innerHTML = clock;
        if ( timer > 0 ) {
            // Time still remains, call this function again in 1 sec
            setTimeout("countdown('" + id + "'," + timer + ")", 1000);
        } else {
            // Time is out! Hide the countdown
            document.getElementById(id).style.display = 'none';
        }

    }
</script>

レコードごとにjsカウンターを機能させることができるように助けてくださいありがとう

4

2 に答える 2

0

The id for HTML element is expected to be unique, you can use class instead if you want to select all wanted element by one name.

<span id="countdown">

You can add class:

<span class="countdown">
于 2013-04-02T11:13:05.677 に答える