[英]Electron app stops rendering while running Javascript code
我目前正在開發一個電子應用程序,該應用程序實質上是我控制的媒體服務器的DDNS啟動器。 基本上,它會檢查Internet連接,獲取服務器的當前IP,然后在系統的默認瀏覽器中將其打開。 但是,我寫的啟動屏幕完全壞了。
每當我在系統上啟動該應用程序時(使用終端上的npm),它都會加載框架,但是圖像會凍結在1/3點處加載。 在HTML主頁面底部的腳本執行完畢之前,它不會加載其余圖像。
我有什么想念的嗎? 如果需要,我可以提供代碼摘錄。
源代碼摘錄:
<script> function wait(ms) { var start = new Date().getTime(); var end = start; while (end < start + ms) { end = new Date().getTime(); } } const isOnline = require('is-online'); const isReachable = require('is-reachable'); const { shell } = require('electron'); window.onload = function() { // Main Script console.log('before'); wait(3000); document.getElementById('progresstext').innerHTML = "Testing connection..."; bar.animate(0.15); // Number from 0.0 to 1.0 wait(250); var amIOnline = false; if (isOnline()) { amIOnline = true; } console.log("Internet Test Ran"); if (!amIOnline) { document.getElementById('errortext').innerHTML = "ERROR: No internet connection. Check the internet connection."; document.getElementById('progresstext').innerHTML = "ERROR"; } var isEmbyReachable = false; if (isReachable('******')) { isEmbyReachable = true; document.getElementById('progresstext').innerHTML = "Connection Test: Passed"; //=> true } //Open Emby in the default browser if (amIOnline && isEmbyReachable) { shell.openExternal("*****"); } }; </script>
Pastebin鏈接到完整資源: https : //pastebin.com/u1iZeSSK
謝謝
開發系統規格:macOS Mojave 10.14,最新穩定的電子版
問題出在您的wait
函數中,因為節點js是單線程,您的等待函數阻塞了您的進程。 您可以嘗試以下代碼。 但是我真的建議您看一下如何在JavaScript中編寫異步函數,並以setInterval和setTimeout作為開始。
但是暫時您可以嘗試使用此代碼。
window.onload = function () {
// Main Script
console.log('before');
// wait 3 seconds
setTimeout(function () {
document.getElementById('progresstext').innerHTML = "Testing connection...";
bar.animate(0.15); // Number from 0.0 to 1.0
// wait 250 mills
setTimeout(function () {
var amIOnline = false;
if (isOnline()) {
amIOnline = true;
}
console.log("Internet Test Ran");
if (!amIOnline) {
document.getElementById('errortext').innerHTML = "ERROR: No internet connection. Check the internet connection.";
document.getElementById('progresstext').innerHTML = "ERROR";
}
var isEmbyReachable = false;
if (isReachable('******')) {
isEmbyReachable = true;
document.getElementById('progresstext').innerHTML = "Connection Test: Passed";
//=> true
}
//Open Emby in the default browser
if (amIOnline && isEmbyReachable) {
shell.openExternal("*****");
}
}, 250)
}, 3000)
};
您可能不會在JavaScript中等待一段時間或任何其他阻塞循環,因為它會阻塞所有其他執行,包括頁面渲染。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.