Php java脚本计时器问题

Php java脚本计时器问题,php,javascript,timer,Php,Javascript,Timer,我在timer.php中有以下代码。如果我运行文件timer.php,我会得到如下正确的输出 time left : 02 min 30 sec 这一数字在不断下降。 但是如果只使用time left:将文件timper.php包含在其他文件中,则在Fire fox中不会显示实际的计时器倒计时。但如果我在ie中运行相同的东西,则显示正确 有什么问题?我的timer.php具有流畅的代码 <script > var sec = 00; // set the seconds

我在timer.php中有以下代码。如果我运行文件timer.php,我会得到如下正确的输出

time left : 02 min 30 sec
这一数字在不断下降。 但是如果只使用time left:将文件timper.php包含在其他文件中,则在Fire fox中不会显示实际的计时器倒计时。但如果我在ie中运行相同的东西,则显示正确

有什么问题?我的timer.php具有流畅的代码

<script >
    var sec = 00;   // set the seconds
    var min = 02  // set the minutes

    function countDown() {
        sec--;
        if (sec == -01) {
            sec = 59;
            min = min - 1;
        } else {
            min = min;
        }
        if (sec<=9) { sec = "0" + sec; }
        time = (min<=9 ? "0" + min : min) + " min and " + sec + " sec ";
        if (document.getElementById) { theTime.innerHTML = time; }
        SD=window.setTimeout("countDown();", 1000);
        if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD);   }
    }

    function addLoadEvent(func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function() {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        }
    }

    addLoadEvent(function() {
        countDown();
    });

</script>

time left :
<span id="theTime" ></span>

有人能在几分钟内给我一个javascript倒计时代码,我可以在我的项目中复制pase吗?

以防它不是打字错误或缩写:你必须在文件名周围加引号

<?php require 'timer.php' ?>

否则,php将查找计时器和php,当找不到它们时,它会将它们解释为两个单独的字符串,并将它们连接起来,因为这会导致两个关于缺少常量的警告,并且如果此代码JS适用于您,则需要在计时器和php之间不带点的“timerphp”

,您的window.onload有问题

<script >

 var countDown = function(idEl) {
  this.sec = 00;   // set the seconds
  this.min = 02;   // set the minutes
  this.el = idEl;  // set the minutes
  this.SD = null;

  this.timer = function(pthis) {
   pthis.sec--;
   if (pthis.sec == -01) {
    pthis.sec = 59;
    pthis.min = pthis.min - 1;
   } else {
    pthis.min = pthis.min; //??
   }
   if (pthis.sec<=9) { pthis.sec = "0" + pthis.sec; }
   var time = (pthis.min<=9 ? "0" + pthis.min : pthis.min) + " min and " + pthis.sec + " sec ";
   if (document.getElementById) { document.getElementById(pthis.el).innerHTML = time; }
   pthis.SD=window.setTimeout(function(){pthis.timer.apply(pthis, [pthis])}, 1000);
   if (pthis.min == '00' && pthis.sec == '00') { pthis.sec = "00"; window.clearTimeout(pthis.SD); }
  };

  this.timer(this);
 }

</script>

time left : <span id="theTime" ></span>
<script>countDown("theTime")</script>

这是我的JS倒计时计时器版本供参考,我在Firefox中测试了你的版本,它对我来说运行良好

<html>
    <head>
        <title>Timer</title>
        <script type="text/javascript">
            var Timer = {
                // Main method for counting down
                countDown: function (displayID, min, sec, callback) {
                    // Update timer display
                    document.getElementById(displayID).innerHTML = (min < 10 ? "0" : "") + min + ":" + (sec < 10 ? "0" : "") + sec;

                    // If there is time left on the timer, continue counting down
                    if (sec > 0 || min > 0) {
                        setTimeout(function () { Timer._countDownCallback(displayID, min, sec, callback); }, 1000);
                    }
                    // When time has run out invoke option callback function
                    else if (typeof callback == "function") {
                        callback();
                    }
                },

                // Internal method for processing remaining time
                _countDownCallback: function (displayID, min, sec, callback) {
                    // Update time left for the timer
                    sec = (sec > 0 ? sec - 1 : 59);
                    if (sec == 59) min = (min > 0 ? min - 1 : 59);

                    // Call main method to update display, etc.
                    Timer.countDown(displayID, min, sec, callback);
                }
            };

            // Start the timer when the page loads
            window.onload = function () {
                Timer.countDown("timerDisplay", 2, 30, function () { alert("The time has come!"); });
            }
        </script>
    </head>
    <body>
        <div>Time left: <span id="timerDisplay"></span></div>
    </body>
</html>

您可以使用这个类。

很可能timer.php中的javascript没有正常运行,因为将该文件包含在另一个文件中会导致HTML由于某些HTML标记不匹配而中断。或者,外部HTML页面中可能存在另一个错误,该错误导致javascript中断。如果没有更多的细节,很难说清楚,因为您所展示的Javascript代码——尽管我不太喜欢,而且有很多问题——应该按照您的预期工作。

Huh,您需要解释更多,向我们展示更多代码您至少应该发布实际的timer.php代码,告诉我们这是一个javascript问题。是的,请添加timer.php代码,这样我们就可以看到它是如何返回的,以及代码的功能。你问了很多问题,但只接受了三个答案。如果你收到一个有帮助的答案,请接受它或至少投赞成票。