久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Node .on 方法觸發太多次

Node .on method firing too many times(Node .on 方法觸發太多次)
本文介紹了Node .on 方法觸發太多次的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 Electron 應用程序,它向用戶顯示目錄列表.當用戶單擊一個按鈕時,我的界面腳本 interface.js 會清除容器 div 并向 main.js 發送一條消息.收到消息后,main.js 將目錄掃描成一個文件名數組,并將該數組作為響應返回給 interface.js.Interface.js 使用 .on 方法,在收到響應時觸發并使用數組的內容更新容器 div.

I have an Electron app that presents a directory listing to the user. When the user clicks a button my interface script, interface.js, clears the container div and sends a message to main.js. On receiving the message, main.js scans the directory into an array of filenames and returns the array to interface.js as a response. Interface.js uses a .on method that fires when the response is received and updates the container div with the contents of the array.

這是我第一次真正嘗試使用 Node,就界面行為而言,一切都非常出色!太棒了,才幾個小時,我已經愛上了 Node!

This is my first real attempt at using Node, and as far as interface behaviour went everything worked brilliantly! Wonderful, it's only been a few hours and I'm already loving Node!

但是,在調試/壓力測試時,我將 .on 方法中返回的數組打印到控制臺,并注意到一些奇怪的行為.用戶第一次單擊按鈕時,.on 方法運行一次(通過向控制臺發送一條消息進行驗證).用戶第二次點擊,該方法運行兩次(通過向控制臺發送兩條消息驗證);第三次運行三次,以此類推.

However, while debugging/stress testing I printed the returned array within the .on method to the console and noticed some strange behaviour. The first time the user clicks the button, the .on method runs once (verified by one message to the console). The second time the user clicks, the method runs twice (verified by two messages to the console); the third time it runs three times and so on.

main.js中掃描目錄的函數每次點擊只運行一次,所以問題一定在interface.js中.

The function in main.js that scans the directory only runs once per click, so the issue must be within inteface.js.

我的 main.js 和 interface.js 代碼:

My code for main.js and interface.js:

main.js:

const {app, BrowserWindow, ipcMain} = require('electron');
const fs = require('fs');

...

ipcMain.on( 'list-directory', ( event, directory ) => {
    var files = fs.readdirSync( directory );
    event.sender.send( 'list-directory-reply', files );
});

interface.js

interface.js

var { ipcRenderer, remote } = require( 'electron' );  
var main = remote.require( "./main.js" );

...

button.addEventListener('click', function(){ showDialogue( this ); }, false );

...

showDialogue( select ) {
    // clear the dialogue
    // some other stuff
    ipcRenderer.send( 'list-directory', './files/documents/' );
    ipcRenderer.on( 'list-directory-reply', function( event, contents ) {
        console.log( contents );
        if ( contents.length > 0 ) {
            // add contents to the dialogue
        }
    } );
}

代碼改編自 Electron 網站上的教程.

The code is adapted from a tutorial on the Electron website.

為什么 ipcRenderer.on 會運行多次?是否有可能每次單擊按鈕時都綁定到某些東西,因此運行次數與過去的單擊次數一樣多?我在事件偵聽器函數內和 showDialogue 函數內放置了一個打印語句,在 ipcRenderer 東西之前,但它們每次點擊都只打印一次,所以重復肯定只會出現來自 ipcRenderer.on.

Why does ipcRenderer.on run multiple times? Is it possible that it's bound to something every time the button is clicked, and thus runs as many times as past clicks? I put a print statement inside the event listener function, and inside the showDialogue function before the ipcRenderer stuff, but they both only printed once per click so the repeats are definitely only coming from ipcRenderer.on.

推薦答案

您在每次點擊按鈕后訂閱 ipcRenderer.on 導致多次訂閱.嘗試在 click 事件之外定義 ipcRenderer.on 事件處理程序,它應該可以正常工作.

You are subscribing to ipcRenderer.on after every button click which is causing multiple subscriptions. Try to define the ipcRenderer.on event handler outside click event and it should work fine.

類似的東西 -

button.addEventListener('click', function(){ showDialogue( this ); }, false );


ipcRenderer.on( 'list-directory-reply', function( event, contents ) {
    // ipcRenderer event handler
});

showDialogue(select) {
    ipcRenderer.send( 'list-directory', './files/documents/' );
}

這篇關于Node .on 方法觸發太多次的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to fix BrowserWindow is not a constructor error when creating child window in Electron renderer process(在 Electron 渲染器進程中創建子窗口時如何修復 BrowserWindow 不是構造函數錯誤) - IT屋-程序員軟件開發技術
mainWindow.loadURL(quot;https://localhost:3000/quot;) show white screen on Electron app(mainWindow.loadURL(https://localhost:3000/) 在 Electron 應用程序上顯示白屏)
Electron webContents executeJavaScript : Cannot execute script on second on loadURL(Electron webContents executeJavaScript:無法在第二個 loadURL 上執行腳本)
how to use electron browser window inside components in angular-cli?(如何在angular-cli的組件內使用電子瀏覽器窗口?)
ElectronJS - sharing redux store between windows?(ElectronJS - 在 Windows 之間共享 redux 存儲?)
How to access camera/webcamera inside electron app?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 国产一区二区三区在线 | 中国一级大毛片 | 成人不卡视频 | 自拍视频在线观看 | 精品视频免费 | a免费视频 | 伊人超碰| 久久亚洲一区二区 | 蜜桃精品噜噜噜成人av | 国产一区| 精品欧美色视频网站在线观看 | 日韩在线不卡 | 亚洲精品一区二三区不卡 | 成人av免费 | 欧美v日韩v | 欧美电影在线观看网站 | 日韩视频在线免费观看 | 日本一区二区影视 | 男女视频在线观看 | 欧美精三区欧美精三区 | 97视频成人 | 国产欧美日韩精品一区二区三区 | 成人黄色三级毛片 | 成人在线观看亚洲 | 亚洲精品乱码久久久久久蜜桃 | 中国一级特黄真人毛片 | 日韩一区二区av | 国产成人99久久亚洲综合精品 | 欧美一区二区三区在线播放 | 狠狠天天| 久久国产精品视频 | 国产精品99久久久久久www | 欧美天堂一区 | 成人免费福利视频 | 精品成人在线视频 | 在线伊人| 中文字幕精品一区久久久久 | 精品久久国产 | 成人精品一区二区 | 欧美在线视频网站 | 天天拍天天射 |