函数启动时JavaScript中的秒表
我想制作一个脚本,计算我执行此函数所用的时间(毫秒) 我希望它在页面加载时启动函数启动时JavaScript中的秒表,javascript,html,Javascript,Html,我想制作一个脚本,计算我执行此函数所用的时间(毫秒) 我希望它在页面加载时启动 window.addEventListener('load', function() { if (window.location.pathname == '/checkout/confirm') { sleep(10).then(() => { var checkout = document.evaluate('/html/body/div[4]/div[3]/d
window.addEventListener('load', function() {
if (window.location.pathname == '/checkout/confirm') {
sleep(10).then(() => {
var checkout = document.evaluate('/html/body/div[4]/div[3]/div/div/div/z-grid[1]/z-grid-item[2]/button', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
checkout.singleNodeValue.click();
console.log("Checkout Complete in x ms");
})
}
})
使用
Date.now()
可以轻松完成此操作。只需检查函数前后的时间并减去结果
window.addEventListener('load', function() {
if (window.location.pathname == '/checkout/confirm') {
let start_function = Date.now();
sleep(10).then(() => {
var checkout = document.evaluate('/html/body/div[4]/div[3]/div/div/div/z-grid[1]/z-grid-item[2]/button', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
checkout.singleNodeValue.click();
let end_function = Date.now();
console.log(`Checkout Complete in ${ end_function - start_function } ms`);
})
}
})
我不完全理解这个问题,您可以简单地包装
const start\u time=new Date()之间的任何函数代码>和const time\u take=新日期()-开始时间代码>,以获取执行时间。你到底面临什么问题?