使用JavaScript第2部分测试图像是否正确加载

时间:2013-07-26 13:58:34

标签: javascript jquery html image-loading

我发现代码在previous question上效果很好,并且mplungan有答案。

然而,代码输出将所有图像分组为单个<div id>

如何将每个“服务器”图片拆分为自己的<div id>

我有一张表列出了我的所有无线客户端。我想将属于客户端的<div id>放在表格中的其他信息旁边。

<header>
<script>
    var timeout = 600000; // 10 minutes 
    var servers = {
        server1:'//server1',
        server2:'//server2'
    } // note the lack of comma after the last item

    var tIds = [];
    function loadImage(id) {
        document.getElementById(id).src=servers[id]+'/on.gif?timestamp='+new Date().getTime();      
        // make sure it does not cache
                          }
        window.onload=function() { 
            for(var server in servers){
            var img = document.createElement('img');
            img.id=server;    
            img.onload=function(){
            clearTimeout(tIds[this.id]); 
            tIds[this.id]=setTimeout("loadImage('"+this.id+"')",timeout); // run again in 10  minutes  
        } 
        img.onerror=function(){ 
            this.src="off.gif";
            this.title = 'server off at '+new Date();
            clearTimeout(tIds[this.id]); 
            tIds[this.id]=setTimeout("loadImage('"+this.id+"')",timeout); // run again in 10 minutes
        } // off cannot come from the broken server
        document.getElementById('imageContainer').appendChild(img);
        loadImage(server) 
    }
</script>
</header>
<body>
     <div id="imageContainer"></div>
</body>

0 个答案:

没有答案