dou11655853 2016-10-06 17:01
浏览 46

如何使用PHP和jquery更新html中经过时间的时间

How can I update the time elapsed without refreshing the page using jquery,

It checks if the time session isset to be displayed time elapsed in seconds

<?php
if (!isset($_SESSION['start_time'][$pId])) {
    $_SESSION['start_time'][$presentId] = time();
}
$end_time = time();

$timeDiff = $end_time - $_SESSION['start_time'][$pId];
?>

form

<form>
    <input type="hidden" name="time_taken" id="quesId_<?php echo $v->id ?>"
           value="<?php echo $timeDiff ?>">
</form>

This displays the time elapsed in the seconds on each page refresh;

<div class="well timer"><i class=" icon-time"></i> <span
        class="response-time"><?php echo $timeDiff ?> </span>
</div>

script

<script>
    $(document).ready(function () {

        //setTimeout(timeUpdate(), 1000);
        function timeUpdate() {
            var time = $("#quesId_<?php echo $pId ?>").val();
            //alert(time);
            console.log(time);
            $('.response-time').text(time);
            setTimeout(timeUpdate(), 1000);
        }

        timeUpdate();


    });
</script>
  • 写回答

1条回答 默认 最新

  • duanguochi6194 2016-10-06 17:53
    关注

    Thanks @charlietfl

    Error was declaring the variable inside the function that has to be outside the function and the timer has to be incremented in every call;

     var time = $("#quesId_<?php echo $pId ?>").val();
                function timeUpdate() {
                    //alert(time);
                    console.log(time);
                    time++;
                    $('.response-time').text(time);
                    setTimeout(timeUpdate, 1000);
                }
    
                timeUpdate();
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面