刷新图像PHP和Javascript [英] Refresh Image PHP and Javascript

查看:93
本文介绍了刷新图像PHP和Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个名为APE的Comet,Realtime引擎,我正在使用jQuery来刷新PHP图像。最初我加载这样的图像:

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 ..."/>');

设备是我从事件中收到的var,所以例如我得到device1,一切正常(图像实际上是图表),标题更改为device1,并绘制最后5分钟

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

然而,我的问题是我每次收到一个事件在最初的一个之后,日期保持在相同的五分钟时间段。我的设备每次都是相同的,但在我的脚本中我计算5分钟前到当前时间的时间纪元,但是这个脚本似乎没有更新图像。是缓存还是什么?

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");
?>

我可以等待大约10分钟,发送一个事件,但时间段仍然是从初始加载,我认为empty()会清空容器,重新加载图像,因此重新运行脚本。任何建议都会有所帮助!

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!

谢谢

推荐答案

一种常见的方法是将一个普通的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.

'image.php?device=' + device + '&t=' + Math.round((new Date()).getTime() / 1000);

因此它认为它没有缓存图像。 (虽然它会继续任何缓存策略导致此问题)

So that it doesn't think it has a cached image. ( though it will continue whatever cache policy lead to this issue )

这篇关于刷新图像PHP和Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆