当另一个倒数计时器启动时,Javascript时钟停止

时间:2013-12-08 15:35:38

标签: javascript timer

在我的页面中,我有一个主时钟javascript计时器,当我尝试使用倒数计时器加载另一页时停止。当我调用此代码<body onload="setCountDown();">

时,特定的时钟计时器停止

我的头是:

<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="css/screen.css">
<!-- <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.min.js"></script> -->
<script>
    function myfunction(val) {
        //document.write(val);
        var selection = val;
        document.getElementById("searchSelection").innerHTML = 'Type ' + selection;
}
</script>
<?php
if(@$_GET["action"]=="getTime"){
  $time1 = Time();  
  $date1 = date("h:i:s A",$time1);  
  echo $date1; // time output for ajax request
  die();  
}
?>
<script type="text/javascript">   
  window.onload = startInterval;  
  function startInterval() {  
    intervalClock = setInterval("startTime();",0000);  
  }
  function startTime() {
    AX = new ajaxObject("?action=getTime", showTime)
    AX.update(); // start Ajax Request   
  } 
  // CallBack 
  function showTime( data ){
    document.getElementById('clock').innerHTML = data;
  }  
</script>
<script type="text/javascript">
// Ajax Object - Constructor
function ajaxObject(url, callbackFunction) {
  var that=this;
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  };
  this.update =
  function(passData,postMethod) {
    if (that.updating) { return false; }
    that.AJAX = null;
    if (window.XMLHttpRequest) {
      that.AJAX=new XMLHttpRequest();
    }else{
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (that.AJAX==null) {
      return false;
    }else{
      that.AJAX.onreadystatechange = function() {
        if (that.AJAX.readyState==4) {
          that.updating=false;
          that.callback( that.AJAX.responseText, that.AJAX.status, that.AJAX.responseXML, that.AJAX.getAllResponseHeaders() );
          that.AJAX=null;
        }
      };
      that.updating = new Date();
      if (/post/i.test(postMethod)) {
        var uri=urlCall+(/\?/i.test(urlCall)?'&':'?')+'timestamp='+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      }else{
        var uri=urlCall+(/\?/i.test(urlCall)?'&':'?')+passData+'&timestamp='+(that.updating.getTime());
        that.AJAX.open("GET", uri, true);
        that.AJAX.send(null);
      }
      return true;
    }
  };
  var urlCall = url;
  this.callback = callbackFunction || function (){};
}
</script>
<script type="text/javascript">
  var years = <?php echo $time_remaing[0]; ?> 
  var months = <?php echo $time_remaing[1]; ?> 
  var days = <?php echo $time_remaing[2]; ?>  
  var hours = <?php echo $time_remaing[3]; ?>  
  var minutes = <?php echo $time_remaing[4]; ?>  
  var seconds = <?php echo $time_remaing[5]; ?> 



function setCountDown () {

 if ( years == '0' && months == '0' && days == '0' && hours == '0' && minutes == '0' && seconds == '0') {
     document.getElementById("remain").innerHTML = "Auction has expire";
     clearTimeout(SD);
     return;
 } else {

  seconds--;
  if (seconds < 0){
      minutes--;
      seconds = 59
  }
  if (minutes < 0){
      hours--;
      minutes = 59
  }
  if (hours < 0){
      days--;
      hours = 23
  }
  document.getElementById("remain").innerHTML = days+" days, "+hours+" hours, "+minutes+" minutes, "+seconds+" seconds";
  SD=window.setTimeout( "setCountDown()", 1000 );
  if ( years == '00' && months == '00' && days == '00' && hours == '00' && minutes == '00' && seconds == '00') {
       seconds = "00"; 
       clearTimeout(SD);
       window.clearTimeout(SD);
        //window.alert("Time is up. Press OK to continue."); // change timeout message as required
        window.location = "http://www.yourpage.com" // Add your redirect url
    } 

}
}
</script>
</head>

我的页面正文:

<?php
include 'core/init.php';
protect_page();
$time_remaing = countdown(12,9,2013,02,00,00);
?>
<!doctype html>
<html>
<?php
include 'includes/head.php';
?>
<body>
<?php
include 'includes/header.php';
?>
<div id="container">
  <?php
include 'includes/aside.php';
?>
  <h1>Auction for product..</h1>
  <p>product discription&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a picture here</p>
  <p> 
  <div class="box" style="width:600px;">
  <body onload="setCountDown();"> 
    <div id="remain"></div>
  </div>
  </p>
  <h2>Give your offer please</h2>
  <p>

  <form action="" method="post">
    <ul>
      <li>Set a price:<br/>
        <input type="text" name="time_start"/>
      </li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li>
        <input type="submit" value="Submit Offer" name="insert_auction">
      </li>
    </ul>
  </form>
  </p>
  <?php
include 'includes/overall/overallfooter.php';
?>

我尝试了一些解决方案,但它仍无效。

0 个答案:

没有答案