0

I am using google colab to capture webcam video. I am using Promises to start and end the video on click. I want to stop capturing video every 5 seconds using a Promise.

  // start recording
  await new Promise((resolve) => {
    capture.onclick = resolve;
  });
  recorder.start();
  capture.replaceWith(stopCapture);
  // use a promise to tell it to stop recording
  
  // I want to stop recording every 5 seconds here instead of on click
  await new Promise((resolve) => stopCapture.onclick = resolve);

  recorder.stop();
  
Mak
  • 1
  • "stop capturing video every 5 seconds" doesn't make sense. After the video is stopped, why would you need to stop it again? – Roamer-1888 Jun 29 '21 at 20:47
  • @Roamer-1888 sorry If I worded this wrong or did not give enough information. Basically what I am trying to do is record a video for 5 seconds stop save that then record again for 5 seconds and repeat. – Mak Jun 30 '21 at 02:14
  • Sounds like you want to [resolve a promise with an event listener](https://stackoverflow.com/a/68135685/633183)... – Mulan Jun 30 '21 at 02:58

1 Answers1

0

Use a setTimeout in your promise to have it resolve in exactly 5 seconds

const FIVE_SECONDS_IN_MS = 5000;

var fiveSecondPromise = new Promise(resolve => {
  setTimeout(resolve, FIVE_SECONDS_IN_MS)
})

fiveSecondPromise.then(done => {
  // hits here in 5 seconds
})
  • The question says "**every** 5 seconds" so it's possible they're looking for `setInterval`, not just `setTimeout`. But it also says "stop capturing", which is not really a repeatable event. Could just be a language thing. ¯\\_(ツ)_/¯ – Wyck Jun 29 '21 at 18:58
  • Yeah I am trying to set an interval to where it will record for 5 seconds and then stop recording and then start record again – Mak Jun 29 '21 at 20:37
  • @Lukas Sorensen So would I just put the stop capture on click in here this block of code fiveSecondPromise.then(done => { // hits here in 5 seconds //insert here }) – Mak Jun 30 '21 at 02:15