본문 바로가기
MySql

MySQL 페이지 새로 고침에서 재설정되지 않도록 카운트 다운 타이머를 만드는 방법

by 베이스 공부 2021. 2. 6.
반응형

내 프로젝트에 대한 온라인 시험 페이지를 만들고 있습니다. 카운트 다운 타이머가 있지만 페이지 새로 고침시 재설정됩니다. 재설정하지 않도록하려면 어떻게해야합니까? The timer is set by fetching time from db. I am using php mysql. Please help me. 다음은 내 코드입니다.

<?php
   $result=mysql_query("select * from test where testid='29'");
   while($time=mysql_fetch_array($result)){
      $dateFormat  = "d F Y -- g:i a";
      $targetDate  = time() + ($time['duration']*60);
      $actualDate  = time();
      $secondsDiff = $targetDate - $actualDate;
      $remainingDay      = floor($secondsDiff/60/60/24);
      $remainingHour     = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
      $remainingMinutes  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60);
      $remainingSeconds  = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60));
      $actualDateDisplay = date($dateFormat,$actualDate);
      $targetDateDisplay = date($dateFormat,$targetDate);
   }
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Untitled Document</title>
   <script type="text/javascript">
      var days = <?php echo $remainingDay; ?>  
      var hours = <?php echo $remainingHour; ?>  
      var minutes = <?php echo $remainingMinutes; ?>  
      var seconds = <?php echo $remainingSeconds; ?> 
      function setCountDown ()
      {
          seconds--;
          if (seconds < 0){
             minutes--;
             seconds = 59
          }
          if (minutes < 0){
              hours--;
              minutes = 59
          }
          if (hours < 0){
              hours = 23
          }
          document.getElementById("remain").innerHTML = "  "+hours+" hr "+minutes+" min    "+seconds+" sec";
          SD=window.setTimeout( "setCountDown()", 1000 );
          if (minutes == '00' && seconds == '00') { 
              seconds = "00"; window.clearTimeout(SD);
              window.location = "result.php"
          } 

       }
    </script>

</head>
<body onLoad="setCountDown();">
   <div id="remain">
         <?php echo "$remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?>
   </div>

 

해결 방법

 

값을 세션 변수로 읽어와 나중에 사용해야합니다.

<?php
session_start();
if (isset($_SESSION['targetdate'])) {
    // session variable_exists, use that
    $targetDate = $_SESSION['targetdate'];
} else {
    // No session variable, red from mysql
    $result=mysql_query("select * from test where testid='29' LIMIT 1");
    $time=mysql_fetch_array($result);
    $dateFormat = "d F Y -- g:i a";
    $targetDate = time() + ($time['duration']*60);
    $_SESSION['targetdate'] = $targetDate;
}

$actualDate = time();
$secondsDiff = $targetDate - $actualDate;
$remainingDay     = floor($secondsDiff/60/60/24);
$remainingHour    = floor(($secondsDiff-($remainingDay*60*60*24))/60/60);
$remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-         ($remainingHour*60*60))/60);
$remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-    ($remainingHour*60*60))-($remainingMinutes*60));
$actualDateDisplay = date($dateFormat,$actualDate);
$targetDateDisplay = date($dateFormat,$targetDate);

?>

 

참조 페이지 https://stackoverflow.com/questions/11272190

 

 

반응형

댓글