js中如何查看程序运行时间(计时器)
作者:互联网
我们可以在要测试代码的最前面加上:
console.time(“计时器名字”);
测试代码最后加上:
console.timeEnd(“计时器名字”);
如下代码:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script type="text/javascript">
console.time("test");
var sum = 0;
for(var i = 1;i <= 100;i++){
var flag = true;
if(i == 1)
flag = false;
for(var j = 2;j < i;j++){
if(i % j == 0){
flag = false;
break;
}
}
if(flag){
document.write(i + " ");
sum++;
}
}
document.write("<br />质数一共有:" + sum + " 个。");
console.timeEnd("test");
</script>
</head>
<body>
</body>
</html>
标签:console,程序运行,sum,timeEnd,js,test,计时器,var 来源: https://blog.csdn.net/fighting_dream/article/details/113778800