Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

89
Views
More elegant way to handle progress update notifications

Hi i'm currently tracking the uploading of a file (as it compresses) and sending the progress back to the user through the GUI. My code is somewhat ugly-looking. Wondering if you guys might have a better way?

The booleans below are meant to prevent constant updates as i only want to update at certain 'checkpoints'. (in this case below, 25%, 50% and 75%).

let past25 = false;
let past50 = false;
let past75 = false;

ffmpeg(file.path)
.addOption('-hls_time', 10)
.addOption('-hls_list_size', 0)
.addOption('-f', 'hls')
.on('progress', (progress) => {
    let sendUpdate = false;

    if (progress.percent > 25 && past25 == false) {
        sendUpdate = past25 = true;
    } else if (progress.percent > 50 && past50 == false) {
        sendUpdate = past25 = past50 = true;
    } else if (progress.percent > 75 && past75 == false) {
        sendUpdate = past25 = past50 = past75 = true;
    }

    if (sendUpdate) {
        notifyUser(progress)
    }
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Perhaps set an initial threshold and an increment and then use a single check. Also allows you to change the granularity of your updates by just changing the values.

let threshold = 25;
let increment = 25;

ffmpeg(file.path)
  .addOption('-hls_time', 10)
  .addOption('-hls_list_size', 0)
  .addOption('-f', 'hls')
  .on('progress', (progress) => {

    if (progress.percent > threshold) {
      notifyUser(progress);
      // account for skipping multiple 'checkpoints' by rounding to the next highest threshold
      threshold += Math.ceil((progress.percent - threshold) / increment) * increment;
    }

  });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!