javascript——页面在脚本加载之前不加载,并且在完成后为循环控制台结果
我有这个代码,我只是无聊,我想看看破解密码需要多长时间。
<!DOCTYPE html>
<html>
<head>
<title> Password Cracker </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<main>
<h1> Password Cracker </h1>
</main>
<script defer>
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()-_=+{}|:;"<,>.?/';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
var Passwords = [];
var PasswordsTime = [];
for (var count = 0; count < 30; count++){
const d = new Date();
let time = d.getTime();
var pass = makeid(5)
loopone:
for (var i = 33; i <= 126; i++){
for (var j = 33; j <= 126; j++){
for (var k = 33; k <= 126; k++){
for (var l = 33; l <= 126; l++){
for (var m = 33; m <= 126; m++){
if (String.fromCharCode(i, j,k,l, m) === pass){
console.log("Password cracked!\nPassword = " + pass);
Passwords.push(pass);
const e = new Date();
let time2 = e.getTime();
PasswordsTime.push(time2 - time1);
break loopone;
}
}
}
}
}
}
}
var sum = 0;
for (var i = 0; i < PasswordsTime.length; i++){
sum = sum + PasswordsTime[i];
console.log("Password: " + Passwords[i]);
console.log("Time: " + PasswordsTime[i]);
}
console.log("Total Time: " + sum);
console.log("Average time: " + (sum / 30));
</script>
</body>
</html>
代码工作正常,但 h1 直到脚本完成后才会显示,这很长,而且,它在所有循环完成后控制台记录 for 循环中的所有内容,而不是在找到每个密码时,为什么?