亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在全球范圍內按時間同時顯示/隱藏廣告

在全球范圍內按時間同時顯示/隱藏廣告

慕斯709654 2022-09-23 09:22:59
這個想法是這樣的:在阿根廷早上6:00,我希望顯示一個保持活動狀態一小時的公告(圖像),也就是說,它可以是可見的,當它達到60分鐘時,它是隱藏的,也就是說,在早上7:00隱藏。此操作每 7 小時重復一次。因此,我希望它保持隱藏7小時,然后再次重復該操作。在下午2:00,它出現,在下午3:00它隱藏。7小時過去了。它在晚上10點再次出現,在晚上11點隱藏,7個小時過去了,他在早上6點再次出現。我創建了此代碼,以便它可以識別時差并在所有國家/地區同時運行,也就是說,廣告在阿根廷上午6:00發布,同時在洛杉磯展示,即使它是凌晨2:00。但它不起作用。它出現在當時根據國家。注意:代碼中有兩個元素,一個用于在 0:00 顯示的另一個廣告var offset = new Date().getTimezoneOffset() / 60;var horarios1 = [6 + offset, 14 + offset, 22 + offset];var elemento1 = document.getElementById("panel1");var horarios2 = [0 + offset];var elemento2 = document.getElementById("panel2");setInterval(function() { var hora = new Date().getHours(); if (horarios1.includes((hora + offset) % 24)) {   elemento1.style.display = 'block'; } else {   elemento1.style.display = 'none'; } if (horarios2.includes((hora + offset) % 24)) {   elemento2.style.display = 'block'; } else {   elemento2.style.display = 'none'; }}, 1000);<div id="panel1" style="display: none;">PANEL 6, 14, 22</div><div id="panel2" style="display: none;">PANEL 0</div>
查看完整描述

1 回答

?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

您的代碼正在使用腳本時間。Java腳本從用戶機器上獲取時間。因此,當您訪問您的網站時,它將顯示您機器的時間,當我訪問時,它將顯示我的機器的時間。但是,如果您希望全世界的通用時間,即在世界各地顯示廣告阿根廷時間06:00,那么您可以應用以下任一方法。


1. 使用服務器時間


您需要在此處編寫一些后端代碼。顯示來自服務器的時間,以及整個世界的固定時間。細節取決于您使用的后端技術(php / java / python)。


2. 使用第三方接口


使用來自其他網站的 API。就像 worldtimeapi.org/ 一樣。撥打ajax電話,獲取所需位置的時間。您可以使用普通的 java 腳本或使用任何 ajax 庫來執行此操作。在這里,我包括兩種方法:1)普通的javascript和2)使用axios(一個流行的ajax庫)


香草


function getTime(url) {

    return new Promise((resolve, reject) => {

        const req = new XMLHttpRequest();

        req.open("GET", url);

        req.onload = () =>

            req.status === 200

                ? resolve(req.response)

                : reject(Error(req.statusText));

        req.onerror = (e) => reject(Error(`Network Error: ${e}`));

        req.send();

    });

}

現在使用此函數進行 ajax 調用


let url = "http://worldtimeapi.org/api/timezone/America/Argentina/Buenos_Aires";


getTime(url)

    .then((response) => { //the api will send this response which is a JSON

        // you must parse the JSON to get an object using JSON.parse() method

        let dateObj = JSON.parse(response);

        let dateTime = dateObj.datetime;

        console.log(dateObj);

        console.log(dateTime);

    })

    .catch((err) => {

        console.log(err);

    });

斷續器


將公理庫添加到項目中。


axios({

    url:"http://worldtimeapi.org/api/timezone/America/Argentina/Buenos_Aires",

    method: "get",

})

    // Here response is an object. The api will send you a JSON. But axios automatically

    // convert it to an object. So you don't need to convert it manually.

    .then((response) => {

        let dateObj = response.data;

        let dateTime = dateObj.datetime;

        console.log(dateObj);

        console.log(dateTime);

    })

    .catch((err) => {

        console.log(err);

    });

(function () {

    var url =

        "http://worldtimeapi.org/api/timezone/America/Argentina/Buenos_Aires",

        horarios1 = [6, 14, 22],

        elemento1 = document.getElementById("panel1"),

        horarios2 = [0],

        elemento2 = document.getElementById("panel2");


    function getTime(url) {

        return new Promise((resolve, reject) => {

            const req = new XMLHttpRequest();

            req.open("GET", url);

            req.onload = () =>

                req.status === 200

                    ? resolve(req.response)

                    : reject(Error(req.statusText));

            req.onerror = (e) => reject(Error(`Network Error: ${e}`));

            req.send();

        });

    }


    setInterval(function () {

        getTime(url)

            .then((data) => {

                var dateObj = JSON.parse(data);

                var dateTime = dateObj.datetime;

                var hora = Number(dateTime.slice(11, 13));


                if (horarios1.includes(hora)) {

                    elemento1.style.display = "block";

                } else {

                    elemento1.style.display = "none";

                }

                if (horarios2.includes(hora)) {

                    elemento2.style.display = "block";

                } else {

                    elemento2.style.display = "none";

                }

            })

            .catch((err) => {

                console.log(err);

            });

    }, 1000);

})();

     <div id="panel1" style="display: none;">PANEL 6, 14, 22</div>

     <div id="panel2" style="display: none;">PANEL 0</div>

希望有所幫助。不過,要記住的事情很少 -

1. worldtimeapi.org/ 是第三方服務。如果他們選擇終止服務,您的代碼將中斷。但是,如果您使用服務器時間,只要您的服務器正在運行,您的代碼就會運行。

2. 由于 ajax 調用,此代碼無法在堆棧溢出中工作。將代碼復制粘貼到項目中以使其正常工作。

3.如果仍然不起作用,則表示您面臨CORS(跨域策略)問題。閱讀此鏈接,搜索互聯網/ SO。您將找到您的解決方案??鞓返木幋a:)


查看完整回答
反對 回復 2022-09-23
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號