dongtai6741 2015-09-28 03:11
浏览 50
已采纳

JQuery - Live PHP重新加载

I've been having some issues with JQuery and PHP recently. I've been creating a webpage that has a button and a number of times that button has been pressed by anyone ever. Currently, I'm working on using JQuery and PHP to make the button press count updates 4 times a second.

Here is a peek at the code:

index.php:

<!DOCTYPE html>
<html>

<head>
  <title>The Button</title>
  <audio id="click">
      /* Big thanks to freesound.org and user "brnck" for the button click sound!
      You can download the sound at: http://freesound.org/people/brnck/sounds/257357/ */
      <source src="button-click.wav">
  </audio>
</head>
<style>
    #background1 {
        image-rendering:-webkit-optimize-contrast;
        position: absolute;
        top: 65%;
        left: 50%;
        margin-top: -167.5px;
        margin-left: -145px;
        z-index: -1;
    }
    #button {
        image-rendering:-webkit-optimize-contrast;
        position: absolute;
        top: 65%;
        left: 50%;
        margin-top: -167.5px;
        margin-left: -145px;
        z-index: 0;
    }
    /* This CSS class of #button is for the button press animation */
    #button.press {
        -webkit-animation-name: pressAnim; /* Webkit Syntax */
        -webkit-animation-duration: 1s;
        animation-name: pressAnim; /* Standard Syntax */
        animation-duration: .75s;
    }
    #background2 {
        image-rendering:-webkit-optimize-contrast;
        position: absolute;
        top: 65%;
        left: 50%;
        margin-top: -167.5px;
        margin-left: -145px;
        z-index: 1;
    }
    #number {
        text-align: center;
        font-style: "Arial Black";
        font-size: 100px;
    }
    /* Webkit Animation */
    @-webkit-keyframes pressAnim {
        0%   {left:50%; top:65%;}
        50%  {left:50%; top: calc(65% + 18px);}
        100% {left:50%; top:65%;}
    }
    /* Standard Animation */
    @keyframes pressAnim {
        0%   {left:50%; top:65%;}
        50%  {left:50%; top: calc(65% + 18px);}
        100% {left:50%; top:65%;}
    }
</style>
<body>

    <div id="background1">
        <img src="ButtonTop.png" width="290" height="372">
    </div>

    <div id="button">
        <img src="Button.png" width="290" height="372">
    </div>

    <div id="background2">
        <img src="ButtonBottom.png" onMouseDown="press()" width="290" height="372">
    </div>

    <div id="number">
        <?php
            include('reload.php');
        ?>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">

        $(document).ready(function(){
            /* This function is for refreshing the total clicks number to make it live */
            setInterval(function reload() {

                console.log('Reloaded Clicks')
                $('#number').load('reload.php');

            }, 250);

        });

    </script>
    <script>

    /* This function is for the button animation, button sound, & button press delay */
        var button = document.getElementById('button');
        function press() {
            if(button.className == "") {
                button.className = "press";
                document.getElementById('click').play();
                /* send click event to server */
                button.addEventListener("webkitAnimationEnd", removeListener);
            }

        };

        function removeListener(event) {
            button.removeEventListener("webkitAnimationEnd", removeListener);
            button.className = "";
        }
    </script>

</body>

reload.php:

<?php
$my_file = 'clicks.txt';
$handle = fopen($my_file, 'r');
$value = fread($handle,filesize($my_file));
echo $value;
?>

In the JQuery section, there is a log to the console, but when I go to the website, nothing appears in the log, so I assume that the JQuery section gets skipped completely for some reason. Although, there are no errors in the console, so that makes the issue harder to identify...

If you want a look at the website, here is the address, but the uptime isn't exactly stellar, so don't be alarmed if you can't access the website.

  • 写回答

2条回答 默认 最新

  • doujianzi8521 2015-09-28 04:53
    关注

    Your code is not working because you are defining some part of the logic in the script that that loads the jQuery library. The content of the script tag is discarded and replaced by the content of the URL listed as src attribute.

    So you need to move the $(document).ready() to the second block. While you are at it, all of your code should be defined in the ready block anyway for safety since otherwise you can't really control precisely when it will be executed.

    Finally, as Ken Franqueiro and Kaleb Klein pointed out, refreshing your data every 250ms will kill your server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?