Revision: 4410
Author: [email protected]
Date: Wed Mar 23 16:07:45 2011
Log: fix playground clock demo
http://codereview.appspot.com/4306048
the clock demo works fine, but if you run something after running
the clock demo, the js console starts spitting out errors every
second, because the clock demo set an interval handler that's still
firing every second.
afaict, there's no security implication to this leak, since the
interval handler is as sandboxed as it would be if it were just
one gadget on a page full of gadgets, but the unintentional
resource leak is annoying, and maybe caja should automatically
cancel timers when a gadget is unloaded.
in the meantime, this change makes the clock demo cancel the
interval handler when it notices it's been unloaded.
[email protected]
http://code.google.com/p/google-caja/source/detail?r=4410
Modified:
/trunk/src/com/google/caja/demos/playground/examples/clock.html
=======================================
--- /trunk/src/com/google/caja/demos/playground/examples/clock.html Mon Mar
21 12:14:40 2011
+++ /trunk/src/com/google/caja/demos/playground/examples/clock.html Wed Mar
23 16:07:45 2011
@@ -1,13 +1,20 @@
<html>
<head>
<script type="application/javascript">
- function init(){
+var intervalId;
+function init(){
clock();
- setInterval(clock,1000);
+ intervalId = setInterval(clock, 1000);
}
function clock(){
+ // no notification when we get unloaded/replaced, so check here
+ var canv = document.getElementById('canvas');
+ if (!canv) {
+ clearInterval(intervalId);
+ return;
+ }
var now = new Date();
- var ctx = document.getElementById('canvas').getContext('2d');
+ var ctx = canv.getContext('2d');
ctx.save();
ctx.clearRect(0,0,150,150);
ctx.translate(75,75);