2014-11-22 73 views
0

我在Firefox 33.0(Ubuntu 12.04)中遇到了这种奇怪的行为:在代码中出现一些错误之后,所有其他Firefox标签都没有显示任何东西,我需要关闭Firefox并再次打开它。全局变量崩溃firefox

看起来像全局变量(NFilhos和SomaFilhos)没有正确地存储在函数之间?调整窗口大小后发生错误(因此在resizeCanvas函数中)。我的代码减少到最低限度:

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Is this a bug?</title> 
</head> 
<body onload="start()" onresize="resizeCanvas()" style="overflow: hidden"> 
<canvas id="tree" style="border:1px solid #000000;"> 
Seu navegador não suporta o recurso "canvas" do HTML 5. 
</canvas> 
</body> 
<script> 
var canv = document.getElementById("tree"); 
var ctx = canv.getContext("2d"); 
var NFilhos = []; 
var SomaFilhos = []; 

function start() { 
    alert("start"); 
    SomaFilhos[0] = 10; 
    // ... read text file from server, prepare data... 
    resizeCanvas(); // draws the data the first time 
} 

function resizeCanvas() { 
    alert("resize"); // just to see where I am 
    canv.width = window.innerWidth; 
    canv.height = window.innerHeight; 
    alert("no error"); // just to see where I am 
    NFilhos[0] = SomaFilhos[0]; // error? 
    //alert(NFilhos[0]);    
} 
</script> 
</html> 
+0

我只是修改您的警报()来显示'SomaFilhos [0]',它显示为10没有问题 – Dinesh 2014-11-22 01:10:14

+0

它在MacOS上在为我工作的FFX-33,除了所有的变量名神秘葡萄牙语...... – Malvolio 2014-11-22 01:13:41

+0

直接调用一个函数调整大小是一个坏主意(并且在它内部更多地使用'alert' - 使用'console.log'来代替调试输出) - resize'事件触发多次在调整窗口大小的过程中。你应该使用一种技术来延迟函数的执行,类似于这里为'scroll'事件描述的内容:http://ejohn.org/blog/learning-from-twitter/ – CBroe 2014-11-22 02:01:51

回答

1
<!DOCTYPE html> 
<html> 
<head> 
<script src="//jashkenas.github.io/underscore/underscore-min.js"></script> 
<meta charset="utf-8"> 
</head> 

<body onload="start()" onresize="_.debounce(resizeCanvas(), 200)" style="overflow: hidden"> 
<canvas id="tree" style="border:1px solid #000000;"> 
Seu navegador não suporta o recurso "canvas" do HTML 5. 
</canvas> 
</body> 

你需要去抖事件,所以它不是射击每毫秒。取决于您的要求,Underscore的节气门或Underscore的去抖方法是您需要的。

+0

谢谢Elise。我需要在屏幕大小调整后再次绘制我的东西,我如何将我的代码加入我的代码中?我没有明白。 – Rodrigo 2014-11-23 20:51:35

+0

你能帮助我吗? – Rodrigo 2014-12-21 03:06:36

+0

你滥用onresize事件,这就是为什么你的浏览器崩溃 – 2014-12-22 02:07:24