Javascript适用于HTML,但不适用于PHP

Code runs perfectly in HTML file, but when used in PHP file the javascript seems to be the only thing that fails. Does anyone know a reason why?

This code is supposed to make a appear after a certain amount of time with out mouse movement. if the user moves their mouse, the will .fadeOut();. However if the user doesn't move the mouse, then they are redirected to another url after a few more seconds.

 <html>
 <head>

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
 <script type="text/javascript">
 $('#showdiv').fadeOut();

 var timedelay = 1;
 function delayCheck()
 {
 if(timedelay > 5)
 {
 $('#showdiv').fadeIn();
     if(timedelay == 12)
         {
                window.location = "https://www.google.com";
         }  
 timedelay = timedelay+1;
 }

 $(document).mousemove(function() {
 $('#showdiv').fadeOut();
 timedelay = 1;
clearInterval(_delay);
 _delay = setInterval(delayCheck, 900);
 });
  // page loads starts delay timer
 _delay = setInterval(delayCheck, 900)

  </script>

  <style type="text/css">
 #showdiv{
 width: 450px;
 border-radius: 10px;
 padding: 50px;
 border: 2px double gray;
 position: absolute;
    top: 40%;
    left: 40%;
 }

 </style>

 </head>
 <body>
      <div id="showdiv">
          <h2>
             You will be redirected in a few second unless you move your mouse.
          </h2>
       </div>
 </body>

</html>

You gotta understand that JavaScript is usually executed at the user's browser while php is usually executed in the server.

However, if you want to embed JavaScript on a php file, you can still do so after the last ?> php tag.

Since you're loading your jQuery functions at the top of the page you need to surround them with a document ready handler:

<script type="text/javascript">
 $(document).ready(function() {
     $('#showdiv').fadeOut();
     var timedelay = 1;
     function delayCheck()
     {
         if(timedelay > 5)
         {
             $('#showdiv').fadeIn();
             if(timedelay == 12)
                {
                    window.location = "https://www.google.com";
                }  
            timedelay = timedelay+1;
         }
     } // closing the function
     $(document).mousemove(function() {
         $('#showdiv').fadeOut();
         timedelay = 1;
         clearInterval(_delay);
         _delay = setInterval(delayCheck, 900);
     });
     // page loads starts delay timer
     _delay = setInterval(delayCheck, 900)
 });
 </script>

In addition, you're missing a closing bracket for your function.