电子事件多次触发 [英] Electron event fires multiple times
问题描述
我有一页,可以通过jquery加载新元素。我的网页上有事件监听器,例如
I have one page, that load new elements via jquery. I have page that have event listener like
ipcRenderer.send('getlist');
ipcRenderer.once('return:list', function (e, l, wn) {
console.log('a');
for(let i = 0; i < l.length; i++) {
$('.a').append('<div>'+l[i]+wn[i]'</div>');
}
$('.a').append('<div>End</div>');
});
将数据发送到页面的代码:
Code that send data to page:
ipcMain.on('getlist', function (e) {
wn = [];
cfg['a'].forEach(s => {
wn.push(yaml.readSync('charcfg.yaml')['name']);
});
mainWindow.webContents.send('return:list', cfg['a'], wn);
});
在我加载另一个页面并再次加载该页面之前,它的工作原理是完美的。
此事件触发多次。每次我回到此页面时,它都会访问更多次。
And it's works perfectly until I load another page, and again load this page. This event fires multiple times. Each time I come back to this page, more times it goes.
我尝试使用
ipcRenderer.once('someevent', function(e, l){...});
仅一次有效。
此脚本的代码随页面一起加载!
Code with this script loads with page!
抱歉输出
通过jquery加载页面的函数:
Function that loads page via jquery:
const htmlContent = $('.content');
function setContent(s) {
$('.content').animate({
opacity: 0
}, 200, function () {
setTimeout(function () {
htmlContent.load(s + '.html');
$('.content').animate({
opacity: 1
}, 200);
}, 100);
});
}
推荐答案
实际上有两个监听器
它是 ipcRenderer.on
(替换为 .once
)和 jquery.on('click')
绑定到加载页面的按钮。使用 jquery.unbind()。on('click')
修复了此问题。
It's ipcRenderer.on
(replaced to .once
) and jquery.on('click')
that was bound to button that loads page. Fixed this with jquery.unbind().on('click')
.
感谢您的帮助,obermillerk
Thanks for your help, obermillerk.
这篇关于电子事件多次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!