doudong8713 2015-06-07 13:24
浏览 77
已采纳

如何在JavaScript中每两秒执行一次PHP函数?

I have this PHP function called GetTotalItemsPercentage() which gets the amount of entries in my MySQL database and returns a percent based on the maximum amount of entries that are allowed (returns 50, 40, 39 etc.). However, I want this to update a div's height depending on this percentage. I currently have this code, however after the first update, it doesn't update again.

    <script type="text/javascript">
        setInterval(function() {
            $("#filled-jackpot-display").animate({height:'<?php include 'database/config.php'; include 'database/jackpot.php'; echo getPotFilledPercentage(); ?>%'});
        }, 2000);
    </script>

So how would I go about getting it to update every two seconds and constantly change the size of the div? I'm guessing I will have to run a loop in the PHP code and then use echo inside the PHP file to run JavaScript rather than the other way.

  • 写回答

2条回答 默认 最新

  • douchi2022 2015-06-07 13:31
    关注

    PHP executes on server, javaScript on the client, you have to make a new php file with

    <?php include 'database/config.php'; include 'database/jackpot.php'; echo getPotFilledPercentage(); ?>
    

    in it, let's call it 'getTotalIP.php' then you call it with ajax.

    <script type="text/javascript">
        setInterval($.ajax('getTotalIP'), 2000); //Short answer with jQuery
    </script>
    

    If your script is intended to print something then you need more code to parse the answer. A good way to do it is printing the php result in json and parse it with javaScript.

    Here you have more cross-browswer ajax information: http://www.w3schools.com/ajax/ajax_example.asp

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

报告相同问题?

悬赏问题

  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码