第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

WooCommerce 的動(dòng)態(tài)交付時(shí)間計(jì)數(shù)器作為短代碼

WooCommerce 的動(dòng)態(tài)交付時(shí)間計(jì)數(shù)器作為短代碼

PHP
蕭十郎 2023-07-08 17:30:15
我使用 JavaScript 計(jì)數(shù)器為 Wordpress 創(chuàng)建了一個(gè)簡(jiǎn)碼。后端- 計(jì)數(shù)器工作正常:前端- 計(jì)數(shù)器不起作用(沒(méi)有控制臺(tái)錯(cuò)誤...):我的簡(jiǎn)碼代碼:// Delivery Counter Time function bb__delivery_counter_function() {    ?> <script type='text/javascript'>        if (document.getElementById('countdownTimer')) {            pad = function(n, len) { // leading 0's                var s = n.toString();                return (new Array( (len - s.length + 1) ).join('0')) + s;            };                        var timerRunning = setInterval(                function countDown() {                    var target = 12; // 12:00hrs is the cut-off point                    var now = new Date();                                        //Put this in a variable for convenience                    var weekday = now.getDay();                                        if(weekday == 0){//Sunday? Add 24hrs                        target += 24;                    }//keep this before the sunday                                        if(weekday == 6){//It's Saturday? Add 48hrs                        target += 48;                    }                                        //If between Monday and Friday,                     //check if we're past the target hours,                     //and if we are, abort.                    if((weekday>=1) && (weekday<=5)){                        if (now.getHours() > target) { //stop the clock                            target += 24;                        }                                    }當(dāng)我在 Elementor 主題中添加短代碼時(shí),“后端”中的一切都工作正常,因此計(jì)數(shù)器在 Elementor 管理視圖中計(jì)數(shù)。但如果我嘗試通過(guò)前端訪問(wèn)該頁(yè)面,Javascript 將無(wú)法工作。有什么想法為什么會(huì)發(fā)生這種情況嗎?
查看完整描述

1 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

您的代碼中有一些錯(cuò)誤。您需要緩沖 Javascript 才能返回它,因?yàn)樗鼞?yīng)該是短代碼,現(xiàn)在 JS 等待使用 jQuery 加載 DOM(因?yàn)?jQuery 庫(kù)已加載到 WordPress/WooCommerce 中)。我還簡(jiǎn)化了你的代碼:


// Delivery Counter Time

add_shortcode('bb__delivery_counter', 'shortcode_delivery_counter_func');

function shortcode_delivery_counter_func() {

    ob_start(); // Start buffering

    ?>

    <script type='text/javascript'>

    jQuery(function($) {

        function pad(n, len) { // leading 0's

            var s = n.toString();

            return (new Array( (len - s.length + 1) ).join('0')) + s;

        };


        setInterval( function() {

            var target = 12, // 12:00hrs is the cut-off point

                now = new Date(),

                weekday = now.getDay();


            if (weekday == 6) { // On Saturday: Adds 48hrs

                target += 48;

            } 

            // On sundays | And from monday to Friday after the cut-off : Adds 24hrs

            else if ( weekday == 0 || now.getHours() > target ) {

                target += 24;

            }


            var hrs  = (target - 1) - now.getHours(),

                mins = 59 - now.getMinutes(),

                secs = 59 - now.getSeconds();


            if (hrs < 0) hrs = 0;

            if (mins < 0) mins = 0;

            if (secs < 0) secs = 0;


            $('#countdownTimer').html( pad(hrs, 2) + ':' + pad(mins, 2) + '.<small>' + pad(secs, 2) + '</small>' );

        }, 1000 );

    });

    </script>

    <?php


    return '<div id="countdownTimer"></div>' . ob_get_clean(); // return buffered JS with html

}

代碼位于活動(dòng)子主題(或活動(dòng)主題)的functions.php 文件中。經(jīng)過(guò)測(cè)試并有效。


查看完整回答
反對(duì) 回復(fù) 2023-07-08
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)