electron clearInterval不工作电子js如何解决这个问题?[已关闭]

ljo96ir5  于 2023-03-06  发布在  Electron
关注(0)|答案(1)|浏览(173)

17小时前关门了。
Improve this question
clearInterval在此代码中不起作用。如何解决此问题?

var inter;
    ipcMain.on("start-stop",(err,data)=>{
    console.log(data.data)
   
    function start(){
        inter = setInterval(function (){handlegole();}, minsNum * 60 * 1000);
    }
    function stop(){
        clearInterval(inter);
    }
    if(data.data == 'Stop'){
        stop();
    } else if(data.data == 'Start'){
        start();
    }
fhity93d

fhity93d1#

每次调用Electron的IPC start-stop通道时,都会声明inter ...

var inter;

但是因为初始化没有提升(在本例中,在ipcMain.on()函数作用域内)...

function start(){
    inter = setInterval(function (){handlegole();}, minsNum * 60 * 1000);
}

当在stop函数中引用时,inter的值为**undefined**..

function stop(){
    clearInterval(inter);
}

要清楚地定义代码,请尽可能停止使用var
使用letconst确定变量或函数的作用域(在所需的函数或块级作用域内)。
我已经复制了一个简单的工作例子,我相信你正在努力实现。
main.js(主进程)

// Import required electron modules
const electronApp = require('electron').app;
const electronBrowserWindow = require('electron').BrowserWindow;
const electronIpcMain = require('electron').ipcMain;

// Import required Node modules
const nodePath = require('path');

// Prevent garbage collection
let window;

function createWindow() {
    const window = new electronBrowserWindow({
        x: 0,
        y: 0,
        width: 800,
        height: 600,
        show: false,
        webPreferences: {
            nodeIntegration: false,
            contextIsolation: true,
            preload: nodePath.join(__dirname, 'preload.js')
        }
    });

    window.loadFile('index.html')
        .then(() => { window.show(); });

    return window;
}

electronApp.on('ready', () => {
    window = createWindow();
});

electronApp.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        electronApp.quit();
    }
});

electronApp.on('activate', () => {
    if (electronBrowserWindow.getAllWindows().length === 0) {
        createWindow();
    }
});

// ---

let minsNum = 0.008333; // 0.5 second interval
let inter; // Declare outside 'electronIpcMain.on()' function scope so `stop()` function can reference it

electronIpcMain.on('start-stop', (event, controlValue) => {
    function start() {
        console.log('Start..!'); // Testing
        inter = setInterval(function() {handleGoal();}, minsNum * 60 * 1000);
        console.log(inter); // Testing
    }

    function stop() {
        console.log('Stop..!');  // Testing
        clearInterval(inter);
        console.log(inter); // Testing
    }

    (controlValue === 'Start') ? start() : stop();
})

function handleGoal() {
    console.log('handleGoal'); // Testing
}

preload.js(主流程)

// Import the necessary Electron modules
const contextBridge = require('electron').contextBridge;
const ipcRenderer = require('electron').ipcRenderer;

// Exposed protected methods in the render process
contextBridge.exposeInMainWorld(
    // Allowed 'ipcRenderer' methods
    'ipcRenderer', {
        // From render to main
        startStop: (controlValue) => {
            ipcRenderer.send('start-stop', controlValue);
        }
    }
);

index.html(渲染进程)

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Electron Test</title>
        <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';"/>
    </head>

    <body>
        <label for="control">Control: </label>
        <input type="button" id="control" value="Start">
    </body>

    <script>
        let control = document.getElementById('control');

        control.addEventListener('click', () => {
            window.ipcRenderer.startStop(control.value);
            control.value = (control.value === 'Start') ? 'Stop' : 'Start';
        })
    </script>
</html>

相关问题