javascript - 我的 Javascript 代码使用了 100% 的 CPU 并且无法加载
问题描述
我试图用文字制作一个小游戏。运行代码 Game.start() 函数后,我落后了,没有任何反应。我认为这是 for 循环。请帮助我这是我的代码:
class unit {
constructor(health, speed, x, y) {
this.health = health;
this.speed = speed;
this.x = x;
this.y = y;
}
moveTo(xval, yval) {
this.x = xval;
this.y = yval;
}
}
var player = new unit(100, 1, 10, 10);
var unit1 = new unit(50, 1, 1, 12);
var unit2 = new unit(50, 1, 2, 4);
var unit3 = new unit(50, 1, 3, 6);
var i = 0;
var units = 4;
var u = [player, unit1, unit2, unit3];
var k, l, t, r;
function draw(txt) { document.write(txt); };
var Game = {
width : 20, // Largeur
height : 20, // Hauteur
start : function() {
for(t = 0; t < this.height++; t++) {
for(k = 0; k < this.width++; k++) {
for(var x1 = 0; i < units; x1++) {
if(u[k].x != k) {
draw('#');
} else {
draw('%');
}
}
}
}
}
}
解决方案
因为在函数中的第三个for
语句中start
,退出条件依赖于在每个循环上i
更新x1
时,所以你有一个无限循环。
推荐阅读
- linux - 将数据从本地 IBM 的 TSM 移动到 Azure Linux VM
- mongodb - 为什么 cursor.count 方法会在不同版本的 MongoDB 上改变其行为
- javascript - Webpack 5 不显示图像
- processing - 获取当前投影矩阵
- visual-studio-code - 我如何在 vscode 中变得更漂亮更漂亮
- c++ - 如何在完全透明的窗口内绘制部分透明的位图?
- windows - 使用 url 时出现无效键错误。
.insteadOf 在 Windows 上 - selenium-webdriver - 什么是 Rest Assured Api 中的 Filter 接口。它用于传递请求中的参数?
- shopify - 无法在 Shopify 部分通过 Liquid 添加图片
- python - Python Flask SQLAlchemy 中的数据访问对象(DAO)