-
-
Notifications
You must be signed in to change notification settings - Fork 382
feat(FlipClock): add Reset instance method #7657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,9 +1,12 @@ | ||||||
| export function init(id, options) { | ||||||
| export function init(id, invoke, options) { | ||||||
| options = { | ||||||
| ...{ | ||||||
| viewMode: 'DateTime', | ||||||
| startValue: 0, | ||||||
| onCompleted: null | ||||||
| onCompleted: null, | ||||||
| counter: 0, | ||||||
| totalMilliseconds : 0, | ||||||
| countDown: false | ||||||
|
Comment on lines
+7
to
+9
|
||||||
| }, | ||||||
| ...options | ||||||
| } | ||||||
|
|
@@ -12,90 +15,22 @@ | |||||
| return; | ||||||
| } | ||||||
|
|
||||||
| let counter = 0; | ||||||
| let totalMilliseconds = 0; | ||||||
| let countDown = false; | ||||||
| const getDate = () => { | ||||||
| const view = options.viewMode; | ||||||
| countDown = false; | ||||||
| if (view === "DateTime") { | ||||||
| const now = new Date(); | ||||||
| return { | ||||||
| years: now.getFullYear(), | ||||||
| months: now.getMonth() + 1, | ||||||
| days: now.getDate(), | ||||||
| hours: now.getHours(), | ||||||
| minutes: now.getMinutes(), | ||||||
| seconds: now.getSeconds() | ||||||
| }; | ||||||
| } | ||||||
| else if (view === "Count") { | ||||||
| counter += 1000; | ||||||
| totalMilliseconds = counter - options.startValue; | ||||||
| } | ||||||
| else if (view === "CountDown") { | ||||||
| countDown = true; | ||||||
| counter += 1000; | ||||||
| totalMilliseconds = options.startValue - counter; | ||||||
| if (totalMilliseconds < 0) totalMilliseconds = 0; | ||||||
| } | ||||||
|
|
||||||
| const seconds = Math.floor(totalMilliseconds / 1000) % 60; | ||||||
| const minutes = Math.floor(totalMilliseconds / (1000 * 60)) % 60; | ||||||
| const hours = Math.floor(totalMilliseconds / (1000 * 60 * 60)) % 24; | ||||||
| const days = Math.floor(totalMilliseconds / (1000 * 60 * 60 * 24)); | ||||||
| const months = 0; | ||||||
| const years = 0; | ||||||
| return { years, months, days, hours, minutes, seconds }; | ||||||
| }; | ||||||
|
|
||||||
| const getConfig = () => [ | ||||||
| { key: 'years', list: el.querySelector('.bb-flip-clock-list.year'), digits: 4 }, | ||||||
| { key: 'months', list: el.querySelector('.bb-flip-clock-list.month'), digits: 2 }, | ||||||
| { key: 'days', list: el.querySelector('.bb-flip-clock-list.day'), digits: 2 }, | ||||||
| { key: 'hours', list: el.querySelector('.bb-flip-clock-list.hour'), digits: 2 }, | ||||||
| { key: 'minutes', list: el.querySelector('.bb-flip-clock-list.minute'), digits: 2 }, | ||||||
| { key: 'seconds', list: el.querySelector('.bb-flip-clock-list.second'), digits: 2 }, | ||||||
| ]; | ||||||
|
|
||||||
| const setDigits = (list, value, digits, countDown) => { | ||||||
| list.classList.remove('flip'); | ||||||
| for (let i = 0; i < digits; i++) { | ||||||
| const place = digits - 1 - i; | ||||||
| const digit = Math.floor(value / 10 ** place) % 10; | ||||||
| setFlip(list.children[i], digit, countDown); | ||||||
| } | ||||||
| list.classList.add('flip'); | ||||||
| }; | ||||||
|
|
||||||
| const go = () => { | ||||||
| const d = getDate(); | ||||||
| const unitConfig = getConfig(); | ||||||
| unitConfig.forEach(({ key, list, digits }) => { | ||||||
| if (list === null) { | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| setDigits(list, d[key], digits, countDown); | ||||||
| }); | ||||||
| return d; | ||||||
| }; | ||||||
|
|
||||||
| let start = void 0 | ||||||
| let current; | ||||||
|
|
||||||
| const flip = ts => { | ||||||
| if (start === void 0) { | ||||||
| start = ts; | ||||||
| current = go(); | ||||||
| current = go(el, options); | ||||||
| } | ||||||
| const elapsed = ts - start; | ||||||
| if (elapsed >= 1000) { | ||||||
| start = ts; | ||||||
| current = go(); | ||||||
| current = go(el, options); | ||||||
| } | ||||||
|
|
||||||
| if (countDown && current.hours === 0 && current.minutes === 0 && current.seconds === 0) { | ||||||
| options.invoke.invokeMethodAsync(options.onCompleted); | ||||||
| if (options.countDown && current.hours === 0 && current.minutes === 0 && current.seconds === 0) { | ||||||
|
||||||
| if (options.countDown && current.hours === 0 && current.minutes === 0 && current.seconds === 0) { | |
| if (options.countDown && options.totalMilliseconds <= 0) { |
Copilot
AI
Feb 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
init() starts a new requestAnimationFrame loop every time it’s called, but there’s no cancellation/guard for an existing loop on the same element. With the new Reset() API (which re-invokes init), repeated resets will leave prior loops running concurrently (double updates + potential multiple onCompleted invocations). Consider tracking the RAF handle/token per id (e.g., in a module-level map or on the element) and cancel/stop the previous loop before starting a new one; also consider exporting a dispose(id) to stop the loop when the component is removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
XML doc has an empty
<returns>tag on a new public API (Reset). Please either remove the tag or fill it with the standard Task description (e.g., “A Task representing the asynchronous operation.”) to match other public methods’ docs.