久久久久久久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?(如何在電子應用程序中訪問相機/網絡攝像頭?)
主站蜘蛛池模板: 久久久久久av | 国外成人在线视频网站 | 国产欧美精品一区二区三区 | 欧美aaaaaaaaaa | 日韩伦理电影免费在线观看 | 我要看黄色录像一级片 | 操久久 | 亚洲欧美激情精品一区二区 | av网站在线看 | 欧美一卡二卡在线观看 | 香蕉久久a毛片 | 久久久精品一区二区三区 | 日韩一区二区三区在线 | 免费在线精品视频 | 精品视频在线观看 | 最新免费黄色网址 | 久久福利电影 | 91丨九色丨国产在线 | 精国产品一区二区三区 | 日韩精品一区二区三区免费观看 | 91精品麻豆日日躁夜夜躁 | 欧美精品91爱爱 | 国产精品久久影院 | 男人天堂视频在线观看 | 久久久久久久91 | 国产1区 | 日韩精品免费在线观看 | 精精国产xxxx视频在线 | 紧缚调教一区二区三区视频 | 男女免费观看在线爽爽爽视频 | 91免费入口 | 天堂在线1| 亚洲人人 | 精品一区二区三区在线观看国产 | 国产欧美一区二区三区另类精品 | 91av导航 | 超碰免费在 | 久久精品久久久久久 | 91精品国产综合久久久久久漫画 | 欧美精品一区三区 | 日本一本在线 |