试图为测验创建计时器

时间:2016-02-24 10:42:23

标签: javascript php

<table>
    <thead>
        <tr>
            <th>column 1</th>
            <th>column 2</th>
            <th>column 3</th>
        </tr>
    </thead>

    <tbody>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
        <tr>
            <td>value 1</td>
            <td>value 2</td>
            <td>value 3</td>
        </tr>
    </tbody>
</table>

任何人都可以帮助我在var myVar = setInterval(function() { myTimer() }, 1000); var d = 1; function myTimer() { document.getElementById("demo").innerHTML = d++; } 中设置动态计时器吗?

我正在尝试创建一个测验应用程序,我需要为数据库中已有的问题运行计时器。

我必须从数据库中检索一个时间,然后我必须运行一个倒计时器。

2 个答案:

答案 0 :(得分:0)

function myTimer(d) {
  d++;
  document.getElementById("demo").innerHTML = d;
  return d;
}

var d = 1;

var myVar = setInterval(function() {
  d = myTimer(d);
}, 1000);
像这样?

[阅读评论后编辑:]

var endpoint = [php_timestamp_here];
var countdown = setInterval(function() {
  var d = new Date();
  var ts = d.getTime();
  if( ts >= endpoint ){
    // stuff after reach the point...
    clearInterval(countdown);
  }
  // stuff every second
}, 1000);

答案 1 :(得分:0)

这样的事情怎么样?它没有回复计时器是完美的。

var running = false;
var timeToRun = 10000;  // 10 seconds
var startTime;
var timer;
var output = document.getElementById("output");


function start(){
    running = true;
    startTime = new Date();
    timer = setInterval(check, 100);
    output.innerHTML = "Started<br>" + output.innerHTML;
}

function stop(){
    running = false;
    clearInterval(timer);
}

function check(){
    var now = new Date();
    var left =  (startTime - now) + timeToRun;
    output.innerHTML = left + "<br>" + output.innerHTML; 

    if (left < 0){
        stop();
        output.innerHTML = "times up <br>" + output.innerHTML; 
    }
} 

start();
<div id="output">o</div>