How cancel Timeout inside object

31
November 26, 2020, at 06:20 AM

I have the following code:

const timers = []
const timer1 = setTimeout(() => {
console.log('Starting timer2')
const timer2 = setTimeout(() => {
  console.log('Its works')
}, 10000)
timers.push({key:2, id:timer2})
}, 10000);
timers.push({key:1, id:timer1})
function remove(key) {
  for (i = 0; i > timers.length; i++) {
     if (timers[i].key === key) {
     timers = timers.slice(i, 1)
     clearTimeout(timers[i].id)
  }
 }
}

When I call the remove(key) function the code is not removing the timers as expected

Answer 1
const timers = []
const timer1 = setTimeout(() => {
console.log('Starting timer2')
const timer2 = setTimeout(() => {
  console.log('Its works')
}, 10000)
timers.push({key:2, id:timer2})
}, 10000);
timers.push({key:1, id:timer1})
function remove(key) {
   const timer = timers.find(f => f.key === key);
   if (timer) {
     clearTimeout(timer.id);
   }
}
READ ALSO
Google Drive API can not upload file into specific folder

Google Drive API can not upload file into specific folder

I have a unlimited storage google drive account from my school account, so I shared drive with my main google account, I want to use apis of google drive to upload video to that shared drive by an file input formThis is where i want to store the file:

27
How to insert json object inside nested object in a document based on nested object UUID

How to insert json object inside nested object in a document based on nested object UUID

I am working on a small project and I am stuck from last month and looking for an exact query to update my document at the exact location

30
Trying to return from co generator runner in nodejs

Trying to return from co generator runner in nodejs

I want to create a function that runs asynchronous code sequentialyThis function takes parameters and returns a Promise that resolves with a JavaScript object

52
Problem with my ToDoList , findAndRemoveById dont work

Problem with my ToDoList , findAndRemoveById dont work

im doing my course and now making a ToDoList and upgrading it with mongoose but the problem is that in this video everything work fine but for me its showing ,,Argument passed in must be a single String

25