gpt4 book ai didi

javascript - JQuery 选择器运行时间

转载 作者:行者123 更新时间:2023-11-29 15:29:41 25 4
gpt4 key购买 nike

JQuery 选择器在内部是如何工作的?它是一种 js getElementById 函数吗?从 DOM 中获取对象的运行时间是多少?

例如:

<html>
<div class='container'>
<div id='myID'>
<p class="myText">My Text</p>
</div>
</div>
</html>

还有这段 JQuery 代码:

$(document).ready(function(){
$("#myID").click(function(){
$("myText").hide(); //Running time?
});
});

最佳答案

有两种获取执行时间的方法,第一种是console.time (仅用于调试),第二个是 performance.now()

Date.now()performance.now() 的区别 - date.now 舍入到毫秒,perf.now 舍入到微秒

//This will console.log the result
$(document).ready(function(){
var myTimerName = 'myTimer1';
$("#myID").click(function(){
console.time(myTimerName);
$(".myText").hide(function(){
console.timeEnd(myTimerName);
});
});
});

//If you need to export this time, use window.performance.now()
$(document).ready(function(){
$("#myID").click(function(){
var start = performance.now();
$(".myText").hide(function(){
var end = performance.now();
$('#timer').text((end - start) + 'ms');
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<div class='container'>
<div id='myID'>
<p class="myText">My Text</p>
</div>
</div>
<span id="timer"></span>
</html>

关于javascript - JQuery 选择器运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35993251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com