我在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>
我只是修改您的警报()来显示'SomaFilhos [0]',它显示为10没有问题 – Dinesh 2014-11-22 01:10:14
它在MacOS上在为我工作的FFX-33,除了所有的变量名神秘葡萄牙语...... – Malvolio 2014-11-22 01:13:41
直接调用一个函数调整大小是一个坏主意(并且在它内部更多地使用'alert' - 使用'console.log'来代替调试输出) - resize'事件触发多次在调整窗口大小的过程中。你应该使用一种技术来延迟函数的执行,类似于这里为'scroll'事件描述的内容:http://ejohn.org/blog/learning-from-twitter/ – CBroe 2014-11-22 02:01:51