我正在使用jQuery脚本定期更新网页中src
元素的<img>
属性。我遇到的问题是图片并不总是在setInterval
更新,但这只在我的网页上。直接访问摄像头UI会显示实际更新频率更高。
为什么会发生这种情况,是否有更好的解决方案来实现我的目标?
这是脚本:
$(document).ready(function(){
var user = "usename";
var pass = "password";
var IP = "1.2.3.4";
var cameraFeed = "http://" + user + ":" + pass + "@" + IP + "/cgi-bin/camera?resolution=270&quality=1";
var topImage = $('#topImage');
setInterval(function(){
d = new Date();
topImage.attr('src', cameraFeed + "×tamp=" + d.getTime());
}, 5000);
});
HTML:
<img id="topImage" width="274px">
html标记,以防HTML中有更好的解决方案。
相机型号为BB-ST162A
答案 0 :(得分:1)
我希望这会给你提供见解,但如果你需要其他解释,我很乐意回答。 我为我的英语道歉,但这不是我的母语。
评论后修改:
我想到了两个解决方案,但我不确定它们是否有效。
第一个解决方案 - 尝试通过Image对象加载图像asyncronusly:
$(document).ready(function(){
var user = "usename";
var pass = "password";
var IP = "1.2.3.4";
var cameraFeed = "http://" + user + ":" + pass + "@" + IP + "/cgi-bin/camera?resolution=270&quality=1";
setInterval(function(){
d = new Date();
var img=new Image();
img.src=
img.onload = function() {
var topImage = $('#topImage');
topImage.attr('src',this.src );
};
img.source=cameraFeed + "×tamp=" + d.getTime();
}, 1000);
});
第二种解决方案:尝试仅使用Image Object获取连续Feed:
function loadImage(){
var user = "usename";
var pass = "password";
var IP = "1.2.3.4";
var cameraFeed = "http://" + user + ":" + pass + "@" + IP + "/cgi-bin/camera?resolution=270&quality=1";
var d = new Date();
var img=new Image();
img.src=
img.onload = function() {
var topImage = $('#topImage');
topImage.attr('src',this.src );
loadImage(); //here you can put a setTimeout to wait some time before start downloading the next "frame"
};
img.source=cameraFeed + "×tamp=" + d.getTime();
}
$(document).ready(function(){
loadImage();
}