Skip to content

Commit

Permalink
Clock: repeat option
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Sep 18, 2023
1 parent 1b06470 commit a864bc3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
22 changes: 14 additions & 8 deletions assets/clock.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const unit = clock.querySelector("span#unit")
const button = clock.querySelector("button")

const max_value = +clock.dataset.maxValue
const repeat = clock.dataset.repeat === "true"

var t = clock.value = 1
var t = (clock.value = 1)
var starttime = null
var dt = 1

Expand All @@ -29,16 +30,21 @@ analogfront.onanimationiteration = (e) => {
const running_time = (Date.now() - starttime) / 1000
t = Math.max(t + 1, Math.floor(running_time / dt))
if (!isNaN(max_value)) {
t = Math.min(t, max_value)
if (repeat) {
if(t > max_value) {
t = 1
starttime = Date.now()
}
} else {
if (t >= max_value) {
clock.classList.add("stopped")
t = max_value
}
}
}
clock.value = t
clock.dispatchEvent(new CustomEvent("input"))
}

if (t >= max_value) {
clock.classList.add("stopped")
t = 0
}
}
unit.onclick = (e) => {
clock.classList.toggle("inverted")
Expand All @@ -48,6 +54,6 @@ button.onclick = (e) => {
starttime = Date.now()
clock.classList.toggle("stopped")
if (!clock.classList.contains("stopped")) {
t = 1 - 1
t = 1
}
}
40 changes: 22 additions & 18 deletions src/Clock.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
### A Pluto.jl notebook ###
# v0.19.12
# v0.19.27

using Markdown
using InteractiveUtils

# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
end

# ╔═╡ 3689bb1b-23f8-41ae-a392-fb2ee2ec40d7
# ╠═╡ skip_as_script = true
#=╠═╡
Expand All @@ -24,12 +14,6 @@ begin
end
╠═╡ =#

# ╔═╡ e7a070ab-67e7-444b-88d8-87c14aaef046
# ╠═╡ skip_as_script = true
#=╠═╡
names(@__MODULE__)
╠═╡ =#

# ╔═╡ fed9022f-1e8e-4f47-92d8-f99065023d29
import AbstractPlutoDingetjes.Bonds

Expand All @@ -47,6 +31,7 @@ begin
fixed::Bool = false
start_running::Bool = false
max_value::Union{Int64,Nothing} = nothing
repeat::Bool = false

# Clock(interval, fixed, start_running, max_value) = interval >= 0 ? new(interval, fixed, start_running, max_value) : error("interval must be non-negative")
end
Expand All @@ -67,7 +52,7 @@ begin
clock.interval < 0 && error("interval must be non-negative")

result = """
<plutoui-clock class='$(clock.fixed ? " fixed" : "")$(clock.start_running ? "" : " stopped")' data-max-value=$(repr(clock.max_value))>
<plutoui-clock class='$(clock.fixed ? " fixed" : "")$(clock.start_running ? "" : " stopped")' data-max-value=$(repr(clock.max_value)) data-repeat=$(repr(clock.repeat))>
<plutoui-analog>
<plutoui-back>$(cb)</plutoui-back>
<plutoui-front>$(cf)</plutoui-front>
Expand Down Expand Up @@ -105,6 +90,11 @@ end
@bind tick Clock()
╠═╡ =#

# ╔═╡ 4930b73b-14e1-4b1d-8efe-686e31d69070
#=╠═╡
tick
╠═╡ =#

# ╔═╡ 9ecd95f0-d7a5-4ee9-9e18-9d87e5d43ab7
#=╠═╡
tick; rand()
Expand All @@ -126,6 +116,18 @@ tick
fasttick
╠═╡ =#

# ╔═╡ b45005b3-822b-4b72-88ef-3f9fe865de6a
# ╠═╡ skip_as_script = true
#=╠═╡
@bind loopy Clock(0.5, max_value=4, repeat=true)
╠═╡ =#

# ╔═╡ e7a070ab-67e7-444b-88d8-87c14aaef046
# ╠═╡ skip_as_script = true
#=╠═╡
loopy
╠═╡ =#

# ╔═╡ a5f8ed96-136c-4ff4-8275-bd569f0dae40
md"""
## Different constructors
Expand Down Expand Up @@ -194,10 +196,12 @@ a

# ╔═╡ Cell order:
# ╠═06289ad2-9e2f-45b3-9d15-7c5a4167e138
# ╠═4930b73b-14e1-4b1d-8efe-686e31d69070
# ╠═9ecd95f0-d7a5-4ee9-9e18-9d87e5d43ab7
# ╠═d82dae11-b2c6-42b5-8c52-67fbb6cc236a
# ╠═80c6e80e-077a-4e31-9467-788a8c437bfc
# ╠═63854404-e6a5-4dc6-a40e-b09b9f531465
# ╠═b45005b3-822b-4b72-88ef-3f9fe865de6a
# ╠═e7a070ab-67e7-444b-88d8-87c14aaef046
# ╠═3689bb1b-23f8-41ae-a392-fb2ee2ec40d7
# ╠═fed9022f-1e8e-4f47-92d8-f99065023d29
Expand Down

0 comments on commit a864bc3

Please sign in to comment.