php页面加载后开始倒计时

时间:2017-12-07 17:07:13

标签: javascript php jquery ajax

我正在尝试创建一个浏览器培训按钮。当用户点击" Brench Press"按下他的"力量"通过我在表单操作中设置的php文件增加,它检查一些值并将值插入到显示的数据库中。

我的问题和我想要帮助的是实现我的时钟功能,将倒计时从30秒设置为0.这应该是用户必须观看并等待的倒计时,直到他可以&#34 ;列车"再次。所以不知怎的,我需要在加载php文件之后编写这个时钟函数来输出,而不是现在按下on按钮。而且对于时钟不刷新它的页面刷新倒计时但继续。

我尝试做的是创建一个内部函数调用的会话并将其发送到站点,以便在加载php文件后加载,但它似乎无法正常工作。

$_SESSION['CountDownBenchPress'] = "<script> countdown(); </script>";

&#13;
&#13;
var time = 30;
function countdown()
{
  if(time==0)
  {
    window.location.reload();
  }
  else
  {
    tmptime = time;
    dayTime = 24*60*60;
    hourTime = 60*60;
    minutesTime = 60;

    days = Math.floor(tmptime/dayTime);
    tmptime = tmptime - days*dayTime;

    hours = Math.floor(tmptime/hourTime);
    tmptime = tmptime - hours*hourTime;

    minutes = Math.floor(tmptime/minutesTime);
    tmptime = tmptime - minutes*minutesTime;

    seconds = tmptime;
    tidtext="";
    if(days>0)
      tidtext = days+ hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else if(hours>0)
      tidtext = hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else
      tidtext =  minutes + " minutes " + seconds + " seconds";

    document.getElementById('timer').innerHTML = tidtext;
    time--;
    setTimeout("countdown()", 1000);
  }
}
&#13;
<form class="" action="" method="">
            <td data-th="Workout"><input type="submit" onclick="countdown()" name="" value="Bench Press"></td>

            <div id="timer"></div>

          </form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

var time = 30;
function countdown()
{
  if(time==0)
  {
    window.location.reload();
  }
  else
  {
    tmptime = time;
    dayTime = 24*60*60;
    hourTime = 60*60;
    minutesTime = 60;

    days = Math.floor(tmptime/dayTime);
    tmptime = tmptime - days*dayTime;

    hours = Math.floor(tmptime/hourTime);
    tmptime = tmptime - hours*hourTime;

    minutes = Math.floor(tmptime/minutesTime);
    tmptime = tmptime - minutes*minutesTime;

    seconds = tmptime;
    tidtext="";
    if(days>0)
      tidtext = days+ hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else if(hours>0)
      tidtext = hours + " hours " + minutes + " minutes " + seconds + " seconds";
    else
      tidtext =  minutes + " minutes " + seconds + " seconds";

    document.getElementById('timer').innerHTML = tidtext;
    time--;
    setTimeout("countdown()", 1000);
  }
}
<form class="" action="" method="">
            <td data-th="Workout"><input type="button" onclick="countdown()" name="" value="Bench Press"></td>

            <div id="timer"></div>

          </form>

你必须输入input type =“button”而不是提交。那是因为提交会将您重定向到您在表单中写入的操作页面(在您的情况下,没有任何内容)

编辑我没有注意到自动点击部分。要解决此问题,您必须按照以下内容修改body标记

<body onload="document.getElementById('clickme').click()">

和这样的按钮

<input type="button" id="clickme" onclick="countdown()" name="" value="Bench Press">