Logo

dev-resources.site

for different kinds of informations.

setTimeout - max timeout footgun

Published at
11/15/2024
Categories
javascript
webdev
browser
settimeout
Author
chinmaymoghe
Author
12 person written this
chinmaymoghe
open
setTimeout - max timeout footgun

Recently I discovered a footgun in real life, which was related to setTimeout, I had to run a timeout for say 28 days for a sale timer, I had a UTC timestamp for the end day, so with the naive approach, I did this

 const date1 = new Date(timestamp1);

  // Difference in milliseconds
  const timeout = date2.getTime() - Date.now();

setTimeout(()=>{
     // some code to turn off some flags / remove some banner
  },timeout);
Enter fullscreen mode Exit fullscreen mode

To my surprise, this did not work or worked too well, since the code within setTimeout executed without waiting for the timeout, I decided to debug in the browser, and I saw that the control jumps into the setTimeout callback almost instantly.

What's the problem here ?

Looking at MDN page of setTimeout, https://developer.mozilla.org/en-US/docs/Web/API/setTimeout#maximum_delay_value , it was clear that there is a max limit until which setTimeout() will run accurately, specifically
2,147,483,647ms or (24.8 days) or (2**31 - 1) ms, this is because browsers store the delay as a 32-bit signed integer internally.

So whenever you pass in a timeout of more than 24.8 days, there is an integer overflow and the code is executed immediately or rather with a lower timeout duration than expected. That's a bummer, and there is no error !!!

Possible solutions for this problem


const days = 30;
const timeout = days * 24 * 60 * 60 * 1000;
console.log('timeto', timeout);
setTimeout(function () {
  console.log('ticked immediately'); // --> executed almost instantly 
}, timeout);


class LongTimeout {
  constructor(cb, timeout) {
    this.timeStart = document.timeline
      ? document.timeline.currentTime
      : performance.now();
    this.lastAnimationFrame = this.runTimer(cb, timeout);
  }
  runTimer(cb, timeout) {
   if(this.cancelled) return;
    const currTimeStamp = performance.now();
    const elapsed = currTimeStamp - this.timeStart;
    if (elapsed >= timeout) {
      cb();
      window.cancelAnimationFrame(this.lastAnimationFrame);
    } else {
      console.log('tick', elapsed, timeout);
      this.lastAnimationFrame = requestAnimationFrame(() =>
        this.runTimer(cb, timeout)
      );
    }
  }
  cancelTimeout() {
    window.cancelAnimationFrame(this.lastAnimationFrame);
    this.cancelled = true;
    this.lastAnimationFrame = null;
  }
}

const longTimer = new LongTimeout(() => {
  console.log(`Tick after ${timeout}`); // timeout works -> does not execute immediately
}, timeout);

Enter fullscreen mode Exit fullscreen mode
browser Article's
30 articles in total
Favicon
Paypal honey browser extension's coupon scam
Favicon
What is Browser Fingerprinting And How does it Identity Fingerprint?
Favicon
How to Clear Cookies in Flutter Custom Tabs?
Favicon
Browser Quest: Finding the Perfect Alternative to Arc Browser
Favicon
Ensuring Smooth User Experiences: A Comprehensive Guide to Browser Compatibility
Favicon
What Building💻 a WebGL App Taught Me About Debugging
Favicon
Wakaruyo - Japanese Text Translation Browser Extension 🌐
Favicon
Quick Guide to Cookies, Local Storage, Session Storage, and Other Web Storage Mechanisms
Favicon
Chrome Extension Development - Develop minimal app with TypeScript, React, Tailwind CSS and Webpack
Favicon
Downloading the same file 102+ times
Favicon
Working on a browser extension
Favicon
What happens when you type a URL into the browser
Favicon
Kako rešiti probleme sa specifičnim stilovima za različite pretraživače
Favicon
Update 100 of the more than 2024 top Google Chrome extensions: improve your browsing!
Favicon
setTimeout - max timeout footgun
Favicon
Step Into Chrome Extension Development: Easy Setup with TypeScript and Webpack
Favicon
How to install Ferdium on Linux
Favicon
How to Clear Browser Cache on Android
Favicon
Browser Extensions: A Frontend Developer's Guide to Building and Deploying Custom Extensions
Favicon
🚀 How a Browser Renders a Web Page
Favicon
How Do I Turn JavaScript Off Across Multiple Browsers
Favicon
Free Cross Browser Testing Tools to pick in 2025
Favicon
Parallel Programming in Web Browsers
Favicon
Por Que o Arc é o Melhor Navegador para Mac
Favicon
idk but webkit sucks
Favicon
What is this browser?
Favicon
在浏览器中进行文件操作
Favicon
How Does Proxy in Browser Work with Swiftproxy
Favicon
Swiftproxy Proxy in Browser Solution for Safe and Anonymous Browsing
Favicon
How to get rid of fake followers on X (Twitter)

Featured ones: