【问题标题】:Return Page Load Time as an alert doesn't work [duplicate]返回页面加载时间作为警报不起作用[重复]
【发布时间】:2017-01-03 13:41:11
【问题描述】:

我必须做一个小网站,我在那里有一个 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 工作正常,但警报只显示“抱歉” 谁知道问题出在哪里?

【问题讨论】:

  • 是的,alert 不需要多个参数。你需要连接字符串'Sorry for the ' + parseTime + 'ms, etc..'
  • 哇哦。我完全忘记了:D 非常感谢

标签: javascript jquery performance load


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    试试

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-11
        • 1970-01-01
        • 1970-01-01
        • 2018-09-27
        相关资源
        最近更新 更多