gpt4 book ai didi

php - DIV 的自动更新不起作用

转载 作者:行者123 更新时间:2023-11-30 10:41:41 25 4
gpt4 key购买 nike

我用这个脚本重新加载一个ID为news的DIV,

<script type="text/javascript">
var auto_refresh = setInterval(function() {
<? if ($lim >= 5)
$lim = 0;
else
$lim = $lim + 2;

if ($cnt == 1){
$lim = 0;
$cnt += 1;
} ?>

$('#news').load('update.php?lim=<? echo $lim ?>');
}, 10000); // refresh every 10000 milliseconds
</script>

update.php 包含此代码以接收 lim 的值,

$lim=$_GET['lim'];

但每隔 10 秒后,“lim”值将发送为 0。我需要根据脚本中的条件更新“lim”值。

我使用 echo 命令检查了 update.php 中的 $lim 值。它始终为 0。我的代码中有什么错误?

最佳答案

您似乎对什么是服务器端/客户端有点困惑。 $lim 是一个 PHP 变量,因此仅在服务器端可用。您需要将逻辑更改为 javascript。试试这个:

var lim = <? echo $lim ?>; // assuming lim is always a number this will work.
var cnt = 0; // just guessed at what this should be initially.
var auto_refresh = setInterval(function() {
if (lim >= 5)
lim = 0;
else
lim = lim + 2;

if (cnt == 1){
lim = 0;
cnt += 1;
}

$('#news').load('update.php?lim=' + lim);
}, 10000); // refresh every 10000 milliseconds

或者,您可以更改逻辑,在计时器的每次迭代中发出 AJAX 请求,将 lim 变量传递给您的 PHP 并接收新值。

关于php - DIV 的自动更新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10705000/

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