-
Notifications
You must be signed in to change notification settings - Fork 56
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
Request: high precise timer #34
Comments
To my knowledge the Alarms API was added expressly to wake event pages so they could perform background work like fetching feeds for an RSS reader (source). The current 1-minute resolution seems to be an attempt to balance an extension's need to perform periodic work against the browser's desire to keep event pages from loading unnecessarily. @hanguokai, can you share more information about your use case? Why does exposing a high precision timer in a background context make possible? How are alternatives insufficient? |
I explained it at https://bugs.chromium.org/p/chromium/issues/detail?id=1159691 Example 1: Clock App set badge text = "hh:mm", and update it every minute at zero second. e.g. update time at 12:20:00(zero second). But if use alarm api with 1-minute interval, it wake up at 12:20:35, 12:21:35, 12:22:35 ... random delay some seconds(< 60 seconds), it does not guarantee to wake up at seconds-level precise. Not only one user said my clock extension is not accurate, because it delay many seconds. Not only update badge text for current time. My clock extension has a function that announce and/or notification the time at every hour(zero minute, zero second)/30min/20min/15min ... It needs to wake up at zero second. Example 2: Custom Reminders User set a reminder at "hh:mm". Usually, user expect the reminder happen at zero second, not delay too many seconds. For example, a teacher, student used it as clock for testing or alarm. If alarm api can't supply precise alarm, the only way to workaround is that use a MV2 persistent background page with setTimout/setInterval . |
There is a video talk about alarm api 8 years ago https://youtu.be/vdMBihN28NI?t=347 Alarm api may delay some time because they consider performance reason, e.g. there are many alarms created by one or multiple extensions. So they align all alarms that created by all extensions to at most 1 minute as minimum interval. |
If there is a at most 1 minute delay, you can set your alarm one minute before and check the current time. |
This is not a reliable workaround. For example, when the background/SW wake up, it’s 30 seconds early, no way to keep alive another 30 seconds reliably. It may terminate soon. Also, this method is not suitable for updating every minute. |
In a previous extension I worked on for a growing startup, a core business proposition of the platform revolved around accurately tracking time spent on tabs. It was pretty important for this information to be as accurate as possible, as small differences could be actionable. Out of the myriad approaches we tried, the notion of running timers per-tab (e.g., in content scripts) ended up being nonperformant (as well as insufficiently accurate) -- the lack of inherent sync meant that it was prone to over/undercounting, and/or it was an extremely convoluted process to try to meaningfully sync the results. The most effective timing approach (for both accuracy and performance) involved setting up a centralized timer in They were a great team, and from time to time, I've regretted leaving. But now that MV3 doesn't seem to offer any clear way of enabling their software, I'm a little glad that I didn't stay. |
We discussed this at our in-person meeting in San Diego. In general, alarms should be fairly accurate and fire with significantly less than 30 seconds of delay. In the cases where it doesn't - for example if the background context is running a blocking loop or showing an alert() - we would not be able to avoid it even with a new high precision permission. The best approach in this case would be to have a regular alarm to make sure your background context is alive, and within the background context use a setInterval which can run with higher frequency. Also, a tangential note - Chrome recently made a change to reduce the minimum frequency of alarms to 30 seconds: https://developer.chrome.com/docs/extensions/whats-new#chrome_120_minimum_alarm_granularity_reduced_to_30_seconds. |
WebKit is also changing to a 30 second minimum interval. WebKit/WebKit#26259 |
This is not a case for this issue.
This is not true. You're a nice guy, but technically you don't really understand this issue. Let me illustrate this issue with code. function getTime(number) {
return new Date(number).toLocaleTimeString();
}
chrome.alarms.onAlarm.addListener(alarm => {
let now = Date.now();
let diff = now - alarm.scheduledTime;
console.log(`${alarm.name} delay: ${diff}ms, expect: ${getTime(alarm.scheduledTime)}, now: ${getTime(now)}`);
});
async function test() {
await chrome.alarms.clearAll();
let now = Date.now();
console.log(`Start Time: ${getTime(now)}`);
// alarm A
let expectTime = [];
for (let i = 1; i < 6; i++) {
expectTime.push(getTime(now + 30000*i));
}
console.log(`Alarm-A expect: ${expectTime.join(', ')}`);
chrome.alarms.create("alarm-A", {
periodInMinutes: 0.5
});
// delay 15s for alarm B
expectTime = [];
for (let i = 1; i < 6; i++) {
expectTime.push(getTime(now + 15000 + 30000*i));
}
console.log(`Alarm-B expect: ${expectTime.join(', ')}`);
setTimeout(() => {
chrome.alarms.create("alarm-B", {
periodInMinutes: 0.5
});
}, 15000);
}
// start test
test(); You may see this output:
Note that in the output above, alarm-B is always 15 seconds behind schedule. This is a reproducible result. If you run Note that you must run test in a packed extension (.crx file) or run code in an extension installed from Chrome Web Store, because there is no alarm restriction in an unpacked extension (dev mode). You can pack an extension with extension-update-testing-tool, and drag and drop the .crx file to a Chromium (Chrome doesn't allow to use an unknow .crx file now). Note that you can create two extensions to reproduce this issue. For example, in extension-A create alarm-A, then delay 15s in extension-B create alarm-B. Alarms in different extensions can cause this problem too, they influence each other. This issue has existed since the beginning of the alarm API, as it was designed in this way. To further explain, the current minimum time schedule (30 seconds or 60 seconds in the past) is to manage all the alarms of all extensions together. So there will be this problem. The goal of #34 is to support alarms that get rid of this limitation. For example:
|
@oliverdunk Adding
follow-up: chrome
|
I'm slightly reluctant to change the label given the current state of this issue. We're open to considering reducing the granularity with which we check alarms and the minimum interval a developer can specify. We don't plan to add any new properties like I think leaving the opposed label, and additionally follow-up for the Chromium change makes sense? If we wanted to clarify the ask in the issue I'd be comfortable changing the label then, but I'm not sure that's worth the effort. Open to thoughts though. |
As mentioned in the meeting, if the minimum interval is reduced to 1 second, then this problem is solved without requiring any API changes (such as a new permission or parameter). But if Chrome don't want to reduce the interval to 1 second, then we need to a proposal to support high-precision (delay of less than a few seconds) alarms. |
alarms
api only support "1-minute timer" and may delay some time(e.g. 30 seconds). So this is not a precise timer, it cannot be used to do clock-like functions.I suggest in manifest.json add a new "highPreciseAlarm" permission, that supply precise timer function.
The text was updated successfully, but these errors were encountered: