jQuery代码不起作用,但也未显示任何错误

时间:2019-12-01 00:49:10

标签: javascript php jquery ajax

我有一些jQuery代码用于调用一个单独的PHP文件(该文件还包含一个jQuery倒计时时钟),尽管根本没有显示任何错误,但似乎没有加载jQuery代码的一部分。

我用来调用文件stats_bar.php 的代码是:

<?php
    if(isset($_SESSION['uid'])){
        include("safe.php");
        include("cron_update_stats.php");
?>

        <div id="stats_bar"></div>

        <script>

        // Stats Bar Script - Currently Not Working

        $(document).ready(function() {
            function loadlink() {
                $.get('ajax/stats_bar.php', '', function(res) {
                    res = $(res).filter('#stats_bar');
                    $('#stats_bar').replaceWith(res);
                });
            }
            loadlink();

            setInterval(function(){
                loadlink(); // this will run after every 1 second
            }, 1000);
        });

        </script>

<?php
    }
?>

stats_bar.php包含以下代码:

<div id="stats_bar">

    <script>
    function startTimer(duration, display) {
        var start = Date.now(),
                diff,
                minutes,
                seconds;
        function timer() {
            // get the number of seconds that have elapsed since 
            // startTimer() was called
            diff = duration - (((Date.now() - start) / 1000) || 0);

            // does the same job as parseInt truncates the float
            minutes = (diff / 60) || 0;
            seconds = (diff % 60) || 0;

            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;

            display.textContent = minutes + ":" + seconds; 

            if (diff <= 0) {
                // add one second so that the count down starts at the full duration
                // example 10:00 not 09:59
                start = Date.now() + 1000;
            }
        }
        // we don't want to wait a full second before the timer starts
        timer();
        setInterval(timer, 1000);
    }

    $(document).ready(function() {
        var tenMinutes = 60 * 10,
        display = document.querySelector('#time');
        startTimer(tenMinutes, display);
    });
    </script>

    <?php $clock = "<span id=\"time\"></span>"; ?>

    <header class="sticky-top">     

        <div class="d-lg-none d-xl-none">
            <div class="container-flex pt-2 pb-2" style="background-color: #191919">
                <div class="container"> 
                    <div class="justify-content-left text-light d-flex flex-column">
                        <div class="pl-md-3 pr-md-3"><b class="text-primary">TURNS &raquo;</b> <?php echo number_format($stats['turns']); ?> <?php if($stats['turns'] < 250){ echo "<small class='text-warning'>(+5 turns in $clock)</small>"; }else{ echo "<small class='text-danger'>(Max. turns)</small>"; }?></div>
                         <div class="pl-md-3 pr-md-3"><b class="text-primary">BANK &raquo;</b> Gold: <?php echo number_format($stats['bankgold']); ?> &#8901; Food: <?php echo number_format($stats['bankfood']); ?></div>
                         <div class="pl-md-3 pr-md-3"><b class="text-primary">RESOURCES &raquo;</b> Gold: <?php echo number_format($stats['gold']); ?> &#8901; Food: <?php echo number_format($stats['food']); ?></div>
                          <div class="pl-md-3 pr-md-3"><b class="text-primary">INCOME &raquo;</b> Workers: <?php echo number_format($unit['worker']); ?> &#8901; Farmers: <?php echo number_format($unit['farmer']); ?></div>
                          <div class="pl-md-3 pr-md-3"><b class="text-primary">BATTLE &raquo;</b> Warriors: <?php echo number_format($unit['warrior']); ?> &#8901; Defenders: <?php echo number_format($unit['defender']); ?> &#8901; Thieves: <?php echo number_format($unit['thief']); ?></div>
                          <div class="pl-md-3 pr-md-3"><b class="text-primary">WEAPONS &raquo;</b> Swords: <?php echo number_format($weapon['sword']); ?> &#8901; Shields: <?php echo number_format($weapon['shield']); ?></div>
                      </div>
                  </div>
              </div>
         </div>

         <div class="d-none d-lg-block">
             <div class="container-flex pt-3 pb-3" style="background-color: #191919">
                 <div class="container"> 
                     <div class="d-flex flex-wrap justify-content-left text-light">
                         <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">TURNS &raquo;</b> <?php echo number_format($stats['turns']); ?> <?php if($stats['turns'] < 250){ echo "<small class='text-warning'>(+5 turns in $clock)</small>"; }else{ echo "<small class='text-danger'>(Max. turns)</small>"; }?></div>
                         <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">BANK &raquo;</b> Gold: <?php echo number_format($stats['bankgold']); ?> &#8901; Food: <?php echo number_format($stats['bankfood']); ?></div>
                         <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">RESOURCES &raquo;</b> Gold: <?php echo number_format($stats['gold']); ?> &#8901; Food: <?php echo number_format($stats['food']); ?></div>
                         <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">INCOME UNITS &raquo;</b> Workers: <?php echo number_format($unit['worker']); ?> &#8901; Farmers: <?php echo number_format($unit['farmer']); ?></div>
                         <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">BATTLE UNITS &raquo;</b> Warriors: <?php echo number_format($unit['warrior']); ?> &#8901; Defenders: <?php echo number_format($unit['defender']); ?> &#8901; Thieves: <?php echo number_format($unit['thief']); ?></div>
                         <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">WEAPONS &raquo;</b> Swords: <?php echo number_format($weapon['sword']); ?> &#8901; Shields: <?php echo number_format($weapon['shield']); ?></div>
                     </div>
                 </div>
             </div>
         </div>

     </header>
</div>

当使用include('ajax/stats_bar.php');而不是jQuery代码来调用PHP文件时,除了PHP文件中的jQuery倒数时钟的行为与jQuery相同之外,其余所有功能均按预期工作用于调用PHP文件的代码,但是我还检查了倒计时时钟代码是否有错误,并且也没有显示任何内容,所以我现在很困惑?

这里有什么我想念的东西吗?

更新

当内容位于单独的PHP文件中时,可以通过使用GET和POST来使用selfagency给出的正确答案,但是,我也能够使用正确答案中提供的信息来产生相同的结果仅使用主页即可实现效果,这意味着在加载内容之间没有延迟-我已在下面添加了代码。

请注意,由于我在这两种方法中均发现,此处获得的结果可能并不是最好的总体结果,因为页面不断刷新PHP的各个部分,因此不断请求数据,请求的数量和整体带宽是天文的,以至于已经包含少量PHP请求的页面很快就会崩溃。

我将继续寻找更好的解决方案!

<?php
                if(isset($_SESSION['uid'])){
                include("safe.php");
                include("cron_update_stats.php");
                    $currenttime = time();
                    $time = date('i:s', (10 * 60) - ($currenttime % (10 * 60)));
                    $bankgold_r = number_format($stats['bankgold']);
                    $bankfood_r = number_format($stats['bankfood']);
                    $gold_r = number_format($stats['gold']);
                    $food_r = number_format($stats['food']);
                    $worker_r = number_format($unit['worker']);
                    $farmer_r = number_format($unit['farmer']);
                    $warrior_r = number_format($unit['warrior']);
                    $defender_r = number_format($unit['defender']);
                    $thief_r = number_format($unit['thief']);
                    $sword_r = number_format($weapon['sword']);
                    $shield_r = number_format($weapon['shield']);
                    $turns_r = number_format($stats['turns']);
            ?>

            <script>
                setInterval("my_function();",1000); 
                function my_function(){
                    $('#refresh_countdown_clock').load(location.href + ' #countdown_clock');
                    $('#refresh_bankgold').load(location.href + ' #bankgold');
                    $('#refresh_bankgold_m').load(location.href + ' #bankgold_m');
                    $('#refresh_bankfood').load(location.href + ' #bankfood');
                    $('#refresh_bankfood_m').load(location.href + ' #bankfood_m');
                    $('#refresh_gold').load(location.href + ' #gold');
                    $('#refresh_gold_m').load(location.href + ' #gold_m');
                    $('#refresh_food').load(location.href + ' #food');
                    $('#refresh_food_m').load(location.href + ' #food_m');
                    $('#refresh_worker').load(location.href + ' #worker');
                    $('#refresh_worker_m').load(location.href + ' #worker_m');
                    $('#refresh_farmer').load(location.href + ' #farmer');
                    $('#refresh_farmer_m').load(location.href + ' #farmer_m');
                    $('#refresh_warrior').load(location.href + ' #warrior');
                    $('#refresh_warrior_m').load(location.href + ' #warrior_m');
                    $('#refresh_defender').load(location.href + ' #defender');
                    $('#refresh_defender_m').load(location.href + ' #defender_m');
                    $('#refresh_thief').load(location.href + ' #thief');
                    $('#refresh_thief_m').load(location.href + ' #thief_m');
                    $('#refresh_sword').load(location.href + ' #sword');
                    $('#refresh_sword_m').load(location.href + ' #sword_m');
                    $('#refresh_shield').load(location.href + ' #shield');
                    $('#refresh_shield_m').load(location.href + ' #shield_m');
                    $('#refresh_turns').load(location.href + ' #turns');
                    $('#refresh_turns_m').load(location.href + ' #turns_m');
                }
            </script>

     <header class="sticky-top">        

        <div class="d-lg-none d-xl-none">
        <div class="container-flex pt-2 pb-2" style="background-color: #191919">
            <div class="container"> 
            <div class="justify-content-left text-light d-flex flex-column">
            <div class="pl-md-3 pr-md-3"><b class="text-primary">TURNS &raquo;</b> <span id="refresh_turns_m"><span id="turns_m"><?php echo $turns_r; ?> <?php if($stats['turns'] < 250){ echo "<small class='text-warning'>(+5 turns in ".$time.")</small>"; }else{ echo "<small class='text-danger'>(Max. turns)</small>"; }?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">BANK &raquo;</b> Gold: <span id="refresh_bankgold_m"><span id="bankgold_m"><?php echo $bankgold_r; ?></span></span> &#8901; Food: <span id="refresh_bankfood_m"><span id="bankfood_m"><?php echo $bankfood_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">RESOURCES &raquo;</b> Gold: <span id="refresh_gold_m"><span id="gold_m"><?php echo $gold_r; ?></span></span> &#8901; Food: <span id="refresh_food_m"><span id="food_m"><?php echo $food_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">INCOME &raquo;</b> Workers: <span id="refresh_worker_m"><span id="worker_m"><?php echo $worker_r; ?></span></span> &#8901; Farmers: <span id="refresh_farmer_m"><span id="farmer_m"><?php echo $farmer_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">BATTLE &raquo;</b> Warriors: <span id="refresh_warrior_m"><span id="warrior_m"><?php echo $warrior_r; ?></span></span> &#8901; Defenders: <span id="refresh_defender_m"><span id="defender_m"><?php echo $defender_r; ?></span></span> &#8901; Thieves: <span id="refresh_thief_m"><span id="thief_m"><?php echo $thief_r; ?></span></span></div>
            <div class="pl-md-3 pr-md-3"><b class="text-primary">WEAPONS &raquo;</b> Swords: <span id="refresh_sword_m"><span id="sword_m"><?php echo $sword_r; ?></span></span> &#8901; Shields: <span id="refresh_shield_m"><span id="shield_m"><?php echo $shield_r; ?></span></span></div>
            </div>
            </div>
        </div>
        </div>

        <div class="d-none d-lg-block">
        <div class="container-flex pt-3 pb-3" style="background-color: #191919">
            <div class="container"> 
            <div class="d-flex flex-wrap justify-content-left text-light">
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">TURNS &raquo;</b> <span id="refresh_turns"><span id="turns"><?php echo $turns_r; ?> <?php if($stats['turns'] < 250){ echo "<small class='text-warning'>(+5 turns in ".$time.")</small>"; }else{ echo "<small class='text-danger'>(Max. turns)</small>"; }?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">BANK &raquo;</b> Gold: <span id="refresh_bankgold"><span id="bankgold"><?php echo $bankgold_r; ?></span></span> &#8901; Food: <span id="refresh_bankfood"><span id="bankfood"><?php echo $bankfood_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">RESOURCES &raquo;</b> Gold: <span id="refresh_gold"><span id="gold"><?php echo $gold_r; ?></span></span> &#8901; Food: <span id="refresh_food"><span id="food"><?php echo $food_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">INCOME UNITS &raquo;</b> Workers: <span id="refresh_worker"><span id="worker"><?php echo $worker_r; ?></span></span> &#8901; Farmers: <span id="refresh_farmer"><span id="farmer"><?php echo $farmer_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">BATTLE UNITS &raquo;</b> Warriors: <span id="refresh_warrior"><span id="warrior"><?php echo $warrior_r; ?></span></span> &#8901; Defenders: <span id="refresh_defender"><span id="defender"><?php echo $defender_r; ?></span></span> &#8901; Thieves: <span id="refresh_thief"><span id="thief"><?php echo $thief_r; ?></span></span></div>
            <div class="pl-2 pl-md-3 pr-2 pr-md-3 pt-1 pb-1"><b class="text-primary">WEAPONS &raquo;</b> Swords: <span id="refresh_sword"><span id="sword"><?php echo $sword_r; ?></span></span> &#8901; Shields: <span id="refresh_shield"><span id="shield"><?php echo $shield_r; ?></span></span></div>
            </div>
            </div>
        </div>
        </div>

     </header>

1 个答案:

答案 0 :(得分:1)

jQuery load方法将不会执行HTML片段中包含的JavaScript。尝试改用get方法,并使用回调将片段添加到页面。

尝试:

// main.php

<div id="stats_bar"></div>

<script>
  $(document).ready(function() {
    function loadlink() {
        $.get('ajax/stats_bar.php', function(res) {
            res = $(res).filter('#stats_bar');
            $('#stats_bar').replaceWith(res);
        });
    }
    loadlink();
  });
</script>
// statsbar.php

<div id="stats_bar">
  <h1>Test</h1>
  <script>
    console.log('it works!');
  </script>
</div>

通知更改自:

<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">

<script>

您需要将<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">放入文档<head>中,并仅在代码块的开头使用<script>

正在加载的PHP文件中的JavaScript似乎也有一些错误。替换为:

<script language="JavaScript" type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">

仅带有<script>

然后在底部更改:

window.onload = function() {
  var tenMinutes = 60 * 10,
    display = document.querySelector('#time');
  startTimer(tenMinutes, display);
};

收件人:

$(document).ready(function() {
  var tenMinutes = 60 * 10,
    display = document.querySelector('#time');
  startTimer(tenMinutes, display);
});

POST方法示例:

    function loadlink() {
        const data = {
          stats: {
            [...]
          }
        }

        $.post('ajax/stats_bar.php', data, function(res) {
            res = $(res).filter('#stats_bar');
            $('#stats_bar').replaceWith(res);
        });
    }
相关问题