我在Firefox 33.0(Ubuntu 12.04)中遇到了这种奇怪的行为:在代码中出现一些错误之后,所有其他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 :(得分: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>
你需要对事件进行去抖动,因此它不会每毫秒发射一次。根据您的要求,您需要下划线的油门或下划线的去抖方法。