Coder Social home page Coder Social logo

hyperapp-synced-interval's Introduction

HyperApp Synced Interval

"I want this function to be called once per second, exactly at the start of the second, with multiple hosts agreeing on when the second is"

Example in demo.html - you can open this on mutliple computers, even with skewed clocks - each instance will sync its own time with the server, and then change the page colour based on what it thinks the current time is. If everything is working, all the devices should change to the same colour at the same moment.

Usage:

SyncedInterval({
    server: "https://shish.io/time.json",  // should return current time in seconds
    interval: 1000, // call onInterval once per second, on the second
    sync: 60*1000,  // try to sync clocks every minute, call onSync when we are synced
    samples: 5,     // take an average of the past 5 samples
    onInterval(state, now) {
        console.log("According to the server, the time is", now);
        return state;
    },
    onSync(state, offset) {
        console.log("We are", offset, "milliseconds away from server time");
        return state;
    },
});

Notes for implementing the server side: the server returns time in seconds, as a floating point, because that's what nearly all languages use. Javascript does everything in milliseconds.

hyperapp-synced-interval's People

Contributors

shish 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.