Return 页面加载时间提醒不起作用

Return Page Load Time as an alert doesn't work

我必须做一个小网站,我在那里有一个 javascript,returns 控制台中的页面加载时间。 那很好用。但是当我尝试用警报做同样的事情时,它不会工作。 我的代码:

$(window).load(function(){
 setTimeout(function(){
 window.performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || 
{};
 var timing = performance.timing || {};
 var parseTime = timing.loadEventEnd - timing.responseEnd;
 console.log('Ladezeit: ', parseTime, 'ms');
 alert('Sorry for the ', parseTime, 'ms, till the website was completely loaded.')
 }, 0);
});

console.log 工作正常,但只显示警报 "Sorry for the" 有人知道问题出在哪里吗?

尝试

let alertString= 'Sorry for the ' + parseTime + 'ms, till the website was completely loaded.';
alert(alertString);

您正在向警报传递 3 个参数,而不是包含您的响应时间的字符串。

您必须使用 + 符号连接 Javascript 中的字符串(和变量)。 试试这个:

alert('Sorry for the ' + parseTime + 'ms, till the website was completely loaded.');

您错误地使用了 alertconsole.log可以接受一组参数,alert只接受一个字符串作为参数。

连接字符串以在警报中使用它:

'Sorry for the ' + parseTime + 'ms, etc..'