Public
Edited
Nov 23, 2024
Insert cell
Insert cell
Insert cell
Insert cell
viewof hz = Inputs.range([1, 10], { label: "Hz", step: 1, value: 4 })
Insert cell
interval = 1000 / hz
Insert cell
Inputs.button("Start", { reduce: start })
Insert cell
Inputs.button("Stop", { reduce: stop })
Insert cell
Insert cell
mutable l = []
Insert cell
function callback() {
mutable l = [mutable t, mutable dt];
}
Insert cell
Insert cell
mutable t = Date.now()
Insert cell
mutable dt = 0
Insert cell
mutable _timeoutId = null
Insert cell
mutable _expectedNextTickTimestamp = mutable t + interval
Insert cell
Insert cell
function start() {
mutable t = Date.now();
mutable _expectedNextTickTimestamp = Date.now() + interval;

mutable _timeoutId = setTimeout(step, interval);
}
Insert cell
function step() {
mutable t = Date.now();
mutable dt = mutable t - mutable _expectedNextTickTimestamp;

if (mutable dt > interval) {
// something really bad happened. Maybe the browser (tab) was inactive?
// possibly special handling to avoid futile "catch up" run
}

callback();

// Start next iteration, take drift into account
mutable _expectedNextTickTimestamp += interval;
mutable _timeoutId = setTimeout(step, Math.max(0, interval - mutable dt));
}
Insert cell
function stop() {
if (mutable _timeoutId) {
clearTimeout(mutable _timeoutId);
mutable _expectedNextTickTimestamp = null
mutable _timeoutId = null;
}
}
Insert cell
Insert cell
Insert cell
window.innerHeight
Insert cell
mutable __listening = false
Insert cell
screenHeight = {
if (!mutable __listening) {
mutable __listening = true

window.addEventListener('resize', (event) => {
mutable __innerHeight = window.innerHeight
})
}

return __innerHeight
}
Insert cell
mutable __innerHeight = window.innerHeight
Insert cell
Insert cell
import { toCss, cssTag } from '@jerryjappinen/templates'
Insert cell
toCss({ foo: 'bar' })
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more