普通网友 2019-01-11 13:57
浏览 49
已采纳

为什么JavaScript代码不会在内容产品wordpress中循环

in content-product.php

i have products as auction and i want show in shop page countdown timer when auction end

i write script code for countdown timer

but the script show for one product not loop for all products

<?php
    date_default_timezone_set("Europe/Oslo");
    $auctionstatus = get_post_meta( $product->get_id(), 'mp_auction_status', true );
    $clientt_tz = date("Y-m-d H:i:s");
    $auctio_time_1= $_product_meta['auction_stop_time'][0];
    if ($auctionstatus == "enabled") {
        if ($auctio_time_1 >= $clientt_tz ){
    echo  "Auction end at ";

    $js_code =<<<JS
    <script language="JavaScript">
    TargetDate = "$auctio_time_1";
    ForeColor = "#009fe3";
    CountActive = true;
    CountStepper = -1;
    LeadingZero = true;
    DisplayFormat = "%%D%% D, %%H%% T, %%M%% M, %%S%% S.";
    FinishMessage = "It is finally here!";
    </script>
    <script language="JavaScript" src="https://scripts.hashemian.com/js/countdown.js"></script>
    JS;

    echo "<br>", $js_code;

        }
        else {
            echo  "Auction end " ;

        }



    } ?>
  • 写回答

1条回答 默认 最新

  • doutinghou6980 2019-01-11 14:02
    关注

    There are two basic reasons:

    1. You are using global variables which will be shared between instances of the script
    2. The script generates an element with a hard-coded ID. An ID must be unique in a document. Each instance of the script calls getElementById and finds the first matching element.

    I'd rewrite this so the data was initialised from data-* attributes on the <script> element and it used the last script element in the document to identify where to insert the span (which it would keep a reference to instead of searching with gEBId each time).

    <p>First</p>
    <script data-number=5>
      (function() {
        const scripts = document.querySelectorAll("script");
        const script = scripts[scripts.length - 1];
        const span = document.createElement('span');
        script.parentNode.insertBefore(span, script);
        span.textContent = script.dataset.number;
        setTimeout(update, 1000);
        function update() {
            let num = parseInt(span.textContent, 10);
            num--;
            span.textContent = num;
            if (num > 0) {
                setTimeout(update, 1000);
            }
        }
      }());
    </script>
    
    
    <p>Second</p>
    <script data-number=3>
      (function() {
        const scripts = document.querySelectorAll("script");
        const script = scripts[scripts.length - 1];
        const span = document.createElement('span');
        script.parentNode.insertBefore(span, script);
        span.textContent = script.dataset.number;
        setTimeout(update, 1000);
        function update() {
            let num = parseInt(span.textContent, 10);
            num--;
            span.textContent = num;
            if (num > 0) {
                setTimeout(update, 1000);
            }
        }
      }());
    </script>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题