【问题标题】:Loading and rendering the contents of the window before displaying alert在显示警报之前加载和渲染窗口的内容
【发布时间】:2018-03-28 17:21:27
【问题描述】:

我是 Electron 的新手(就像英语一样 :)。
在加载和呈现主窗口的内容后,我试图输出一个简单的alert

index.html:

<html>
  <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="styles.css">
    <title>Test</title>
  </head>
  <body>
    ...
    Here's some content and images. A lot of them.
    ...
    <script src="alert.js"></script>
  </body>  
</html>

alert.js:

//Simple alert with some system information
alert("!@#$%^&*");

但在绘制窗口内容之前会出现警告。 Deferasync 没有帮助(即 &lt;script defer async src="alert.js"&gt;&lt;/script&gt;)。我究竟做错了什么?在我看来,这是一个非常简单和愚蠢的问题,但我找不到答案。

UPD: 我目前找到的唯一方法是使用setTimeout

setTimeout(function(){
     alert("!@#$%^&*");
}, 300);

【问题讨论】:

  • 我认为脚本在您的正文内容被获取并发送到解析后运行,在内容渲染后运行 alert.js,在 jquery 中,您应该尝试$(window).on('load', function(){ alert("sdfsd"); })
  • @Rahul Jain。我还没有熟悉jquery。我在哪里插入此代码?
  • 你不需要 jQuery:window.addEventListener("load",x=&gt;alert("hello world"))
  • @HMR。此代码也显示了在渲染窗口内容之前的警报。

标签: javascript electron


【解决方案1】:

由于您的代码位于页面底部,您可以使用 IIFE(立即调用函数表达式):

(function() {
alert("!@#$%^&*");
}());

您也可以使用 setTimeout:

(function() {
    setTimeout(function () { 
        alert("!@#$%^&*"); 
    } , 2000)
}());

【讨论】:

  • 刚刚看到一个类似的解决方案:)我的意思是setTimeout。但这不是拐杖吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-11
相关资源
最近更新 更多