刷新图像 PHP 和 Javascript
我正在使用名为 APE 的 Comet 实时引擎,并且使用 jQuery 刷新 PHP 图像。最初我像这样加载图像:
<div id="container">
<img src="image.php" style='background: url(../assets/load.gif) no-repeat center center;margin-left:42px;' alt=' Loading ...' width="500px" height="300px" />
</div>
然后当我收到一个事件时,我这样做:
$("#container").empty();
$("#container").html('<img src="image.php?device='+device+'" style="background: url(../assets/load.gif) no-repeat center center;margin-left:42px;" width="500px" height="300px" alt=" Loading ..."/>');
device是我从事件接收到的一个var,所以例如我得到device1,一切正常(图像实际上是一个图表)并且标题更改为“device1”,它绘制了最后 5 分钟
但是,我的问题是每次我收到事件时,在第一个事件之后,日期仍保持在相同的五分钟时间段。我的设备每次都是相同的,但在我的脚本中,我计算了 5 分钟前到当前时间的时间纪元,但该脚本似乎没有更新图像。是被缓存了还是什么?
我尝试在页面顶部使用它:
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
?>
我可以等待大约10分钟,发送一个事件,但时间段仍然是从初始加载开始,我认为“empty()”会清除容器,重新加载图像,因此重新运行脚本。任何建议都会有所帮助!
谢谢
I am using a Comet, Realtime engine called APE, and I am using jQuery to refresh a PHP image. Initially I load the image like this:
<div id="container">
<img src="image.php" style='background: url(../assets/load.gif) no-repeat center center;margin-left:42px;' alt=' Loading ...' width="500px" height="300px" />
</div>
And then when I receive an event I do this:
$("#container").empty();
$("#container").html('<img src="image.php?device='+device+'" style="background: url(../assets/load.gif) no-repeat center center;margin-left:42px;" width="500px" height="300px" alt=" Loading ..."/>');
device is a var that I receive from the event, so for example I get device1, everything works ok (the image is actually a Chart) and the title changes to "device1" and it plots the last 5 minutes
However, my problem is every single time I receive an event, after this initial one, the date remains at the same five minute period. My device is the same each time, but inside my script I calculate the time epoch 5 minutes ago to the current time, but this script doesn't seem to update the image. Is it being cached or something?
I have tried using this at the top of the page:
<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
?>
I can wait about 10 minutes, send an event, but the time period is still from the initial load, I thought that the "empty()" would clear the container out, reload the image and therefore rerun the script. Any advice would help!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在图像路径上添加一个额外的随机变量。
这将使它看起来像一个不同的 URL,并导致浏览器不缓存。这与 YUI 使用的策略相同。
Add an extra random variable onto the image path.
That will make it appear like a different URL and cause the browser not to cache. This is the same tactic used by YUI.
一种常见的方法是将一个简单的 var 添加到保存 unix 时间戳的查询字符串中;浏览器将每次调用视为对唯一图像的调用。例如
,这样它就不会认为它有缓存的图像。 (尽管它将继续导致此问题的任何缓存策略)
A common approach is to add a trivial var to the query string that holds a unix timestamp; The browser sees each call as a call to a unique image. e.g.
So that it doesn't think it has a cached image. ( though it will continue whatever cache policy lead to this issue )