为了账号安全,请及时绑定邮箱和手机立即绑定

控制台会阻止页面渲染

控制台会阻止页面渲染

MMTTMM 2023-07-20 17:16:23
我写了一个html页面,代码如下。<html><header>    <div class="logo"><a href="index.html" title="Coyle Appliance Repair"></a></div>    <div class="phoneNumber"><h3 class="numberHeader">call today for an appointment - same day service is available</h3>        <h1 class="number"><a href="tel:+1-555-555-5555">555-555-5555</a></h1></div>    <div class="greyStrip"><h2 class="motto">Serving the Twin Cities and western Wisconsin since 1982</h2></div></header><script>  ((function demo () {    let a = 1    while (a < 10000) {      a++      console.log(a)    }  })())</script><div class="mainContent"><h2 class="missionStatement">Full service restaurant and commercial kitchen repair. We service all cooking, food prep, warewash/dishroom, and    refrigeration equipment.</h2></div></html>问题是,keep和remove代码有什么区别console.log(a)当我删除它时,页面将流畅地显示,没有任何阻塞或闪烁。但是当我添加这个控制台时,页面会阻塞一段时间或者闪烁。
查看完整描述

3 回答

?
哔哔one

TA贡献1854条经验 获得超8个赞

另外值得注意的是,console.log在旧版浏览器中可能会导致更多致命问题(页面加载失败),因此,如果您在生产中不需要它,请将其删除,因为无论如何它都是不必要的糟粕。



查看完整回答
反对 回复 2023-07-20
?
青春有我

TA贡献1784条经验 获得超8个赞

浏览器正在计算该循环的每次迭代并运行其中的所有代码。每个函数都会使用一些东西,在本例中,您调用该函数console.log 10,000次,这意味着浏览器必须执行至少10,000 次计算。

console.log不会阻止页面渲染,但会阻碍页面渲染的性能,这就是为什么你会得到闪烁效果。

console.log 如何影响性能:console.log 比空函数慢很多


查看完整回答
反对 回复 2023-07-20
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

是的,任何函数调用都会稍微降低性能。而在你的代码中,你调用console.log大约10000次,所以它会让页面的性能降低。



查看完整回答
反对 回复 2023-07-20
  • 3 回答
  • 0 关注
  • 92 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信