Javascript 如何在一段时间后取消图像加载?
我需要能够在设定的时间段后取消图像加载,它需要随后调用onError操作 它将做什么: 尝试检索资源。src=”https://www.site.com/cgi-bin/pullimg.cgi?user=+encodeURI(document.cookie),它提取用户特定的资源。cookie存储在受保护的文件夹中。 如果不能在1秒(1000毫秒)内完成,则执行onError。 OneError更改images src属性,然后重新加载img。(更改为不同的uri,如mirror.site.com/err.png) 此外,它还可以是一个javascript函数(newImage)Javascript 如何在一段时间后取消图像加载?,javascript,html,Javascript,Html,我需要能够在设定的时间段后取消图像加载,它需要随后调用onError操作 它将做什么: 尝试检索资源。src=”https://www.site.com/cgi-bin/pullimg.cgi?user=+encodeURI(document.cookie),它提取用户特定的资源。cookie存储在受保护的文件夹中。 如果不能在1秒(1000毫秒)内完成,则执行onError。 OneError更改images src属性,然后重新加载img。(更改为不同的uri,如mirror.site.co
抱歉,没有提供现有代码;不过我可以用多个语言编写代码。使用window.stop停止下载
if(window.stop !== undefined)
{
window.stop();
}
else if(document.execCommand !== undefined)
{
document.execCommand("Stop", false);
}
更改图像源的步骤
var img = document.getElementBYId("yourImageID");
img.setAttribute("src","newSource.gif");
试试这个:
var image = new Image();
image.src = "https://www.site.com/cgi-bin/pullimg.cgi?user=" + encodeURI( document.cookie );
setTimeout
(
function()
{
if ( !image.complete || !image.naturalWidth )
{
image.src = "http://mirror.site.com/err.png";
}
},
1000
);
您可以使用此代码加载映像,如果在1秒内未成功加载(无论故障是通过OneError、onabort还是从经过的时间),请切换到加载备用映像
function loadImage(url, altUrl) {
var timer;
function clearTimer() {
if (timer) {
clearTimeout(timer);
timer = null;
}
}
function handleFail() {
// kill previous error handlers
this.onload = this.onabort = this.onerror = function() {};
// stop existing timer
clearTimer();
// switch to alternate url
if (this.src === url) {
this.src = altUrl;
}
}
var img = new Image();
img.onerror = img.onabort = handleFail;
img.onload = function() {
clearTimer();
};
img.src = url;
timer = setTimeout(function(theImg) {
return function() {
handleFail.call(theImg);
};
}(img), 1000);
return(img);
}
// then you call it like this
loadImage("https://www.example.com/cgi-bin/pullimg.cgi?user=" + encodeURI(document.cookie), "http://mirror.site.com/err.png");
你能发布你的代码吗?我没有任何完整的代码。谢谢!它似乎在工作。现在我也知道更多关于html/js的内容。我想这缺少了一个开头的花括号。