简体   繁体   English

Chrome中出现警报后如何重新加载javascript中的页面

[英]How to reload page in javascript after an alert in Chrome

I'm running through the MDN canvas game tutorial of the game Breakout. 我正在浏览游戏Breakout的MDN canvas游戏教程。 When the ball passes the paddle, the game should end and the page reload, starting again. 当球通过球拍时,游戏应结束,页面重新加载,重新开始。

The code looks like this: 代码如下:

 if (y + dy < ballRadius) {
     dy = -dy;
 } else if(y + dy > canvas.height-ballRadius) {
     if(x > paddleX && x < paddleX + paddleWidth) {
        dy = -dy;
     } else {
        alert("GAME OVER");
        document.location.reload();
     }
 }

In FireFox and Safari this runs as expected, after dismissing the alert, the page reloads and the script starts over from beginning. 在FireFox和Safari中,此操作按预期运行,在消除警报后,页面将重新加载,脚本从头开始。

However, in Chrome, after the the ball hits the end of the y bounds of the canvas and the alert is displayed, if you hit Ok to dismiss it, the page does not appear to reload. 但是,在Chrome中,当球碰到画布的y边界的末端并显示警报时,如果您单击Ok使其消失,该页面似乎不会重新加载。 Instead, the ball continues to move and after every iteration, a new alert is displayed (which if you dismiss with Ok it continues without reload). 取而代之的是,球继续移动,并且在每次迭代之后,都会显示一个新的警报(如果您单击“ Ok关闭该警报,它将继续而不会重新加载)。

Anyone have any idea what's happening and why the page will not reload? 任何人都知道发生了什么,为什么页面无法重新加载?

Here's the full code 这是完整的代码

The same functionality as in Chrome can be observed in JSFiddle JSFiddle中可以观察到与Chrome中相同的功能

Update: In the console in Chrome, if I try document.location.reload() I get undefined . 更新:在Chrome的控制台中,如果尝试使用document.location.reload() ,则会得到undefined document.location results in localhost as expected. document.location将按预期结果在localhost中。

Clear the interval after you call the reload. 调用重新加载后清除间隔。

Change setInterval(draw, 10); 更改setInterval(draw, 10); to var interval = setInterval(draw, 10); var interval = setInterval(draw, 10); .

Add clearInterval(interval); 添加clearInterval(interval); after document.location.reload(); document.location.reload(); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM