2017-01-03 55 views
0

我必须做一个小网站,我得到了一个JavaScript,它在控制台返回页面加载时间。 那很好。但是,当我尝试用警报完成同样的事情时,它将无法工作。 我的代码:返回页面加载时间作为警报不起作用

$(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工作正常,但警报只显示了“对不起,” 任何人都知道是什么问题?

+1

呀,警报不采取多个参数..你需要连接字符串'“对不起了” +分析时+“毫秒,等等。'' –

+0

哦哇。我完全忘记了:D非常感谢 –

回答

1

你误以为使用alertconsole.log可以接受一组参数,alert只需要一个字符串作为参数。

将字符串连接到警报中使用它:

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

1

尝试

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

您传递3个参数,以提醒,而不是包含你的响应时间的字符串。

1

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

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