Last active
September 17, 2020 10:36
-
-
Save zahqresh/3549cb82a3de075306e89d59b90c1ef5 to your computer and use it in GitHub Desktop.
JS task runner with setInterval there are two interval one runs every 10 seconds and then executes a function is your main task runner when that function is being executed and your tasks are running a check which is a boolean is set to true so it tells the other interval that major task runner function is running a task so dont call it now to ru…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Major Function | |
const MajorLazer = async () => { | |
if(NumberOfTasks != 0){ | |
//Set the check to true so interval which is running majorlazer fun become clear | |
//then don't run the majorlazer multiple times causing errors or makig multiple calls | |
check = true; | |
var count = 0; | |
var myTimer; | |
(async () => { | |
myTimer = setInterval(() => { | |
console.log("Any Task Runs Here!"); | |
//check to stop the tasks runner function which is myTimer | |
if (count >= NumberOfTasksYouHave) { | |
console.log('Clearing the interval...'); | |
clearInterval(myTimer); | |
console.log('Done!'); | |
//Set the check to false so interval which is was stop which is runing majorlazer function starts again | |
check = false; | |
} | |
}, 1000); | |
//This await delay is npm module it delays and pause any function further executing | |
//Wait for interval to be executed and this delay should be equal to delay of interval | |
//Because don't want count to be increamented before anytask finished | |
await delay(1000); | |
count++; | |
})(); | |
}else{ | |
console.log("No new task available!"); | |
} | |
}; | |
//Run this interval to run the main interval which is running the tasks | |
setInterval(() => { | |
//if check is true means one scheduled task is running then concole log anything | |
if (check) { | |
console.log("Waiting for tasks to finished...."); | |
console.log("*********************************"); | |
} else { | |
//Call the MajorLazer() to run task so if task is running | |
} | |
}, 10000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment