Coder Social home page Coder Social logo

timeframer's Introduction

timeframer

setTimeout and setInterval that keeps on ticking when a tab is in the background (they're otherwise throttled). Use with care and only when there's a good use-case.

It works by creating an invisible iframe with a WebWorker that does the actual timers and update ticks via a MessageChannel. This is just a way to make it real simple to avoid cross-domain issues, the iframe and the worker is hosted on the same origin. If you can host your own worker on your own domain, that's much easier.

All this trickery is due to the Microsoft browsers, all others make do fine with Blob URL's.

This is an initial proof-of-concept.

Usage

Include in webpage

From a webpage, make a <script> tag with src pointing to timeframer.js where it and timeframer.html is hosted.

setTimeout, clearTimeout, setInterval and clearInterval will all be available on the global window.timeframer.

You can host it yourself (the dist folder), and it's also hosted on GitHub pages at https://kribblo.github.io/timeframer/timeframer.js, example:

<script src="https://kribblo.github.io/timeframer/timeframer.js"></script>
<script>
    let i = 1;
    let interval = timeframer.setInterval(function() {
        console.log('Interval', i++)
    }, 500)
    timeframer.setTimeout(function() {
        timeframer.clearInterval(interval)
    }, 1600);
</script>

Override window.setTimeout and friends

window.setTimeout = timeframer.setTimeout;
window.setInterval = timeframer.setInterval;
window.clearTimeout = timeframer.clearTimeout;
window.clearInterval = timeframer.clearInterval;

Use caution. If you do this, all timers and intervals in the page will run at full speed in the background, this should only be used where it is needed.

TODO

  • test case/example page

Development

Update dist

npm install
npm run build

Publish to gh-pages

npm run www

timeframer's People

Contributors

kribblo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.