V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yagamil
V2EX  ›  Python

js 定时刷新图片不生效

  •  
  •   yagamil · 2021-01-03 12:00:54 +08:00 · 1051 次点击
    这是一个创建于 1222 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码用网上别人的,结果是进去了定时执行代码,可是图片还是旧的,不会定时刷新.

    <canvas id="myCanvas" width="472" height="472" style="border:1px solid #c3c3c3;">
    	Your browser does not support the canvas element.
    </canvas>
    
    <FONT SIZE=1 id="F1"></FONT>
    <FONT SIZE=1 id="F2"></FONT>
    
    
    
    <script>
    	var i_height = 472;
    	var i_width = 472;
    	var pic = document.getElementById("myCanvas");
    	pic.style.height = i_height + "px";
    	pic.style.width = i_width + "px";
    </script>
    
    <script type="text/javascript">
    	var n = 0;
    	var image = new Image();//放入函数里面:浏览器运行一分钟左右系统内存用到高达 90%,故做一个全局的反冲图片
    	function changeImage() {
    		var canvas = document.getElementById("myCanvas");
    		var cxt = canvas.getContext("2d");
    		//ctx.restore();
    		image.src = "/static/wx_login1.jpg";
    		image.onload = function () //确保图片已经加载完毕
    		{
    			n = n + 1;
    			n = n % 100;
    			document.getElementById("F1").innerHTML = "刷新:" + n;
    			if (image.complete)//如果图片加载完成,绘制
    				cxt.drawImage(image, 0, 0);
    			else
    				alert(image.complete);
    
    		}
    		image.onerror = function () {
    			document.getElementById("F2").innerHTML = "error N:" + n;
    		};
    		//ctx.save();
    	}
    
    	setInterval("changeImage();", 2000);
    </script>
    
    Lax
        1
    Lax  
       2021-01-03 12:20:52 +08:00
    每次执行后 `image.src` 都一样,需要看图片是不是被缓存了。
    可以在 URL 里增加一个随机参数。
    yagamil
        2
    yagamil  
    OP
       2021-01-03 12:26:37 +08:00
    @Lax 谢谢!搞定了. 是这个原因.受教了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   999 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 21:54 · PVG 05:54 · LAX 14:54 · JFK 17:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.