gpt4 book ai didi

javascript - 通过javascript进行图像轮询

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:42 24 4
gpt4 key购买 nike

我需要使用 javascript 轮询图像,并且需要在找到图像后执行操作。这是我为此任务编写的代码。

/*----Image handling script starts here----*/
var beacon = new Image();
beacon.onload = function() {
console.log('Image found');
console.log(this.width,this.height);
window.clearInterval(timer);
};
beacon.onerror = function(){
console.log('Image not found');
}
var timer = window.setInterval(function(){
console.log('sending the request again');
beacon.src = "http://www.google.co.in/logos/2010/lennon10-hp.gif";
},2000);
/*----Image handling script ends here----*/

问题是,在一个 GET 请求之后,每次设置 src 时,响应都会被缓存并且请求不会被发送。如果您检查 NET 选项卡,它仅在第一个 src 集上发送请求并缓存响应。

每次我的代码设置 src 时,我都需要发送一个新的图像请求。有什么解决方法吗?

最佳答案

更改您的 src 以将当前 EPOCH 时间作为变量包含在内。

beacon.src = "http://www.google.co.in/logos/2010/lennon10-hp.gif?" +
date.getTime();

每次在查询字符串中使用不同的变量将错过缓存,因为就浏览器而言,每次请求图像时图像都是不同的(或可能是),并且没有限制你问的次数,因为时间希望不会停止...

关于javascript - 通过javascript进行图像轮询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4488350/

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