markb
markb

Reputation: 1285

Quitting an electron app after setInterval() from external script file

Edited for more clarity.

I am trying to make a splash screen applet - to start learning nodejs and electron.

I want to create an applet that launches, shows some messages every 10 seconds, and then quits.

I'm basing it off the Discord app and Teams where they have pop up loading screens that have a progress bar, and once completed load the full application.

I want to know how to do it before the "load full app" portion kicks in and how to close the splash screen completely.

Currently I have an index.js, index.html, and a main.js.

index.js is the electron browser window. index.html is the main rendered page, and the main.js is the timer to switch the innerHTML based on the time from:

// main.js
var startTime       = 0,
    totalTime       = 10,
    timeBuffer      = 2,
    totalPercent    = 0,
    timeCounter     = setInterval( progress, 1000 );

function progress() {
    if( (startTime += 1) >= (totalTime + timeBuffer + 1) ) {
        // quit app (1)
    } else {
        // show messages here
    }
}

At point (1) in the code, I've tried adding in app.close(); but that fails since I haven't added in app. I tried adding it in but that doesn't work either.

I tried adding in:

// main.js
const { ipcRenderer } = require('electron');
ipcRenderer.send('close-me');

//index.js
ipcMain.on( 'close-me', (evt, arg) => {
    app.quit();
});

But this didn't work either. I'm still trying to understand the relationship between index.js and the other scripts you might write for the app - but thought quitting the app entirely would be easy.

Upvotes: 1

Views: 568

Answers (2)

9pfs
9pfs

Reputation: 640

Store your setInterval()as a variable and use JavaScript clearInterval(variable) as a page exit action.

Upvotes: 0

markb
markb

Reputation: 1285

It seems app.quit() wasn't working in this instance, but app.close(0) was.

Reading through the docs didn't suggest any reasons to why but in case someone else runs into this problem too.

Upvotes: 1

Related Questions