I have a HTML slider with 4 positions. 15, 30, 45, 60 "minutes". Depending on the position I want to execute some code every selected minutes. So I decides to implement a switch case and use node-cron (or node-schedule) to achieve this.
But I am struggling with the cron jobs. So if I start with every 30 minutes and than switch to e.g. 15 minutes there are 2 cron jobs running and so on. I experimented with .stop()
end .start()
(or .cancel()
) but without success.
Can anyone please give me a hint to achieve my goal? Maybe there is a totally different solution...
Thanks so far
const { ipcRenderer } = require("electron");
const cron = require('node-cron');
var slider = document.getElementById("myRange");
var output = document.getElementById("demo");
output.innerHTML = slider.value;
slider.oninput = function () {
output.innerHTML = this.value;
time();
};
function time() {
const slidertime = document.getElementById("myRange").value
switch (parseInt(slidertime)) {
case 15:
console.log("every 15 min");
pattern = '0 */15 * * * *'
break;
case 30:
console.log("every 30 min");
pattern = '0 */30 * * * *'
break;
case 45:
console.log("every 45 min");
pattern = '0 */45 * * * *'
break;
case 60:
console.log("every 60 min");
pattern = '0 */60 * * * *'
break;
}
const task = cron.schedule(pattern, function () {
ipcRenderer.send("asynchronous-message", `foo`);
});
}
time();