gpt4 book ai didi

jquery - 不断刷新/接收输出
问题

转载 作者:行者123 更新时间:2023-11-28 04:59:45 25 4
gpt4 key购买 nike

所以我有这段 JavaScript 代码,用于不断接收我网站中文件的输出

$(function() {
function reloadTable() {
$.get("pot1.php", function(data) {
$("#pot").html(data);
});
}
reload = setInterval(reloadTable, 1000);
});

这是使用id“pot”的div

<div id="pot">
<div id="timeleft">
<h2 class="text-primary text-center"><span class="tl1" id="tl1"><?php include('tl1.php');?></span></h2>
</div>
<div>
<h1>Jackpot:
<font color="#598749"><span class="pot1"><?php include('pot1.php');?></span></font>
</h1>
</div>
<?php include('/spinfunction/raffleanim.php'); ?>
</div>

但由于某种原因它似乎不起作用,有什么帮助吗?

最佳答案

你检查过它没有被浏览器缓存吗?

您是否还确保 AJAX 请求不是 404ing 并且实际上正在接收有效数据?

它看起来非常“可缓存”,因为它是一个 GET 请求并且没有查询字符串。

一些可能会阻止缓存的事情:

  • 从服务器返回 header 以防止缓存
  • 将其更改为 POST 请求
  • 为每个请求附加不同的查询字符串

有关这些的更多信息

标题:

https://stackoverflow.com/a/21609642/5302749

在您的回复之前添加这些:

header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');

POST 请求 https://api.jquery.com/jquery.post/ -

Pages fetched with POST are never cached, so the cache and ifModified options in jQuery.ajaxSetup() have no effect on these requests.

每次附加不同的查询字符串正如@Barmar 提到的,您还可以提供 cache: false 作为选项,我相信某些版本的 jQuery 不允许您将选项直接传递给 $.get,所以我将其更改为 $。 ajax,错误处理也是一个好主意,所以我添加了一个 .fail 回调。

   $(function() {
function reloadTable(){
$.ajax({
url: "pot1.php",
cache: false,
})
.done(function( data ) {
$( "#pot" ).html( data );
})
.fail(function(jqXHR, textStatus, errorThrown){
console.log('Error: ' + textStatus ); // error handling here
});
}
reload = setInterval(reloadTable, 1000);
});

或者手动添加一个参数(这只是看起来更小,因为不包括错误处理)——我认为最好不要重新发明轮子,而是尽可能使用 jQuery 的内置函数。

   $(function() {
var reloadCount = 0;
function reloadTable(){
reloadCount++;
$.get("pot1.php?reloadCount=" + reloadCount, function(data) {
$("#pot ).html(data);
});
}
reload = setInterval(reloadTable, 1000);
});

关于jquery - 不断刷新/接收输出 <div> 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40186749/

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