gpt4 book ai didi

javascript - 使用 JavaScript 刷新 DIV 失败

转载 作者:行者123 更新时间:2023-12-01 08:32:13 25 4
gpt4 key购买 nike

我想在 10 秒内自动刷新某些 DIV(darksky widget id="1")。

我尝试了很多次,但都失败了。请参阅下面并评论逻辑错误。

这是我糟糕的代码。

function autoRefresh_sample_div2() {
var currentLocation2 = window.location;
$("#1").fadeOut('slow').load(currentLocation2 + ' #1').fadeIn("slow");
}
setInterval('autoRefresh_sample_div2()', 10000); //10 seconds
<div class="row col-lg-9">
<div class="col-lg-1">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" style="text-align: center">#</th>
</tr>
</thead>
</table>
</div>
<div class="col-lg-8 mb-2">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" style="text-align: center">Graph</th>
</tr>
</thead>
</table>
</div>
<div class="col-lg-3 mb-2">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" style="text-align: center">Weather NOW</th>
</tr>
</thead>
</table>
</div>
<div class="col-lg-1 my-auto">
<h1 class="display-4 text-center">1</h1>
</div>
<div class="col-lg-8"><iframe class="embed-responsive embed-responsive-21by9" src="http://naver.com/"></iframe></div>
<div class="col-lg-3 my-auto" id="1">
<script type='text/javascript' src='https://darksky.net/widget/default-small/36.2385,127.2047/ca12/en.js?width=100%&height=70&title=Full Forecast&textColor=333333&bgColor=FFFFFF&transparency=false&skyColor=undefined&fontFamily=Default&customFont=&units=ca'></script>
</div>

</div>

最佳答案

  1. 不建议使用数字 ID
  2. 您的负载不会从天气 channel 重新执行 iframe 脚本

所以我拿了天气 JS 来看看它做了什么。

我将他们预期的 ID 添加到您的 div,然后使用随机值重新加载脚本以停止缓存,而不是加载整个页面并从中提取 div。

我尝试附加到头部的“load”事件以淡入,但我使用了超时

const weather = new URL('https://darksky.net/widget/default-small/36.2385,127.2047/ca12/en.js?width=100%&height=70&title=Full Forecast&textColor=333333&bgColor=FFFFFF&transparency=false&skyColor=undefined&fontFamily=Default&customFont=&units=ca');

const reloadWeather = () => {
$('script[id="weather"]').remove();
weather.searchParams.set("rdn", new Date().getTime());
$('<script id="weather">').attr('src', weather).appendTo('head');
setTimeout(() => { $("#customize-script-container").fadeIn("slow") },1000) ;
};

let tId;
$(function() {
tId = setInterval(() => {
$("#customize-script-container").fadeOut('slow', reloadWeather)
}, 10000);
reloadWeather()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row col-lg-9">
<div class="col-lg-1">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" style="text-align: center">#</th>
</tr>
</thead>
</table>
</div>
<div class="col-lg-8 mb-2">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" style="text-align: center">Graph</th>
</tr>
</thead>
</table>
</div>
<div class="col-lg-3 mb-2">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col" style="text-align: center">Weather NOW</th>
</tr>
</thead>
</table>
</div>
<div class="col-lg-1 my-auto">
<h1 class="display-4 text-center">1</h1>
</div>
<div class="col-lg-8"></div>
<div class="col-lg-3 my-auto" id="customize-script-container"></div>

</div>

要拥有多个天气 channel ,您必须获取 API key 、使用该 key 编写服务器端调用或破解其代码。

JS文件包含以下内容

var customContainer = document.getElementById("customize-script-container");
if(customContainer === null)
document.write("<iframe id='ds_b75d85c2dee419f1bfdd9ce0243df27f' type='text/html' frameborder='0' height='70' width='100%' src='https://darksky.net/widget/default-small/36.2385,127.2047/ca12/en?domain="+encodeURIComponent(window.location.href)+"&auth=1581324702_72b56ef770251e4ad0661834a512e6e9&width=100%25&amp;height=70&amp;title=Full%20Forecast&amp;textColor=333333&amp;bgColor=FFFFFF&amp;transparency=false&amp;skyColor=undefined&amp;fontFamily=Default&amp;customFont=&amp;units=ca'></iframe>");
else
document.getElementById("customize-script-container").innerHTML = "<iframe id='ds_b75d85c2dee419f1bfdd9ce0243df27f' type='text/html' frameborder='0' height='70' width='100%' src='https://darksky.net/widget/default-small/36.2385,127.2047/ca12/en?domain="+encodeURIComponent(window.location.href)+"&auth=1581324702_72b56ef770251e4ad0661834a512e6e9&width=100%25&amp;height=70&amp;title=Full%20Forecast&amp;textColor=333333&amp;bgColor=FFFFFF&amp;transparency=false&amp;skyColor=undefined&amp;fontFamily=Default&amp;customFont=&amp;units=ca'></iframe>";

这对我们来说是个坏消息,因为他们使用 document.write。我想绕过这个问题,但想出了一个更酷的解决方案:更新时重命名每个 div

let tId, $divs, cnt = 0;

const reloadWeather = () => {
const $div = $(divs[cnt]);
$('script[id="weather"]').remove();
$("#customize-script-container").prop("id", "temp"); // rename the last div
$div.prop("id", "customize-script-container"); // set the ID expected of the script
const lat = $div.data("lat");
const long = $div.data("long");
let weather = new URL(`https://darksky.net/widget/default-small/${lat},${long}/ca12/en.js?width=100%&height=70&title=Full Forecast&textColor=333333&bgColor=FFFFFF&transparency=false&skyColor=undefined&fontFamily=Default&customFont=&units=ca`);
weather.searchParams.set("rdn", new Date().getTime());
$('<script id="weather">').attr('src', weather).appendTo('head');
setTimeout(() => {
$div.fadeTo("slow","1")
}, 1000);
};


$(function() {
divs = $(".weather");
tId = setInterval(() => {
if (cnt >= divs.length) cnt = 0;
const $div = $(divs[cnt]);
if ($div) {
$div.fadeTo("slow",0, function() {
reloadWeather();
cnt++;
})
} else {
console.log("div", cnt, "not found")
}
}, 10000);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div class="col-lg-3 my-auto weather" data-lat="36.2385" data-long="127.2047"></div>
<div class="col-lg-3 my-auto weather" data-lat="55.1234" data-long="100.1234"></div>

关于javascript - 使用 JavaScript 刷新 DIV 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60146692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com