I’ve a perform inside scheduler that calls the perform after each 19 seconds and the capabilities sends knowledge to the backend.
at any time when i swap the display or decrease the browser recreation tab, The scheduler pauses which i need to keep away from because the delay for which the scheduler does’nt sends the info will get very huge as browser window is minimized or tab is switched and scheduler is paused.
Is there any manner i can keep away from scheduler from getting paused ?
Or Can setInterval present the specified end result ?
Thanks prematurely.
1 Like
I’ve the identical downside.
I attempt to use replace() to repair this challenge.
For a depend down timer, thie challenge will be resolved.
Nonetheless, when utilized to a tween, the identical downside nonetheless happens.
Does anybody have a greater answer?
import { _decorator, Element, director, Node } from 'cc';
const { ccclass, property } = _decorator;
sort CountdownTask = {
targetTime: quantity;
persecondCallback: (remainingSeconds: quantity) => void;
endTimerCallback: () => void;
lastSecond: quantity;
};
@ccclass('ScheduleExtra')
export class ScheduleExtra extends Element {
personal static Occasion: ScheduleExtra;
protected onLoad() {
if (ScheduleExtra.Occasion == null) {
ScheduleExtra.Occasion = this;
director.addPersistRootNode(this.node);
} else if (ScheduleExtra.Occasion != this) {
this.node.destroy();
}
}
personal static duties: CountdownTask[] = [];
replace() {
const now = Date.now();
for (let i = ScheduleExtra.duties.size - 1; i >= 0; i--) {
const job = ScheduleExtra.duties[i];
const remaining = Math.max(0, (job.targetTime - now) / 1000);
const floored = Math.flooring(remaining);
if (floored !== job.lastSecond) {
job.lastSecond = floored;
job.persecondCallback?.(remaining); // 傳原始浮點秒數
}
if (remaining void, endTimerCallback: () => void) {
const newTask: CountdownTask = {
targetTime: Date.now() + seconds * 1000,
persecondCallback,
endTimerCallback,
lastSecond: -1
};
this.duties.push(newTask);
}
}
// Easy methods to use
//personal runTimer0() {
// ScheduleExtra.StartCountdown(
// this.timer0,
// (remaining) => {
// console.log(`Timer0 Remaining:${remaining} second`);
// this.Lbl_Timer0.string = remaining.toString();
// },
// () => {
// console.log("Rely down over!");
// }
// );
// }
sort or paste code right here
Hey @dukeofchiwawa, simply leaping in since I’m going through the identical downside.
Yeah, this occurs as a result of browsers pause or decelerate timers and animations whenever you swap tabs or decrease the window. So issues like scheduler
, replace()
, and even tweens gained’t run correctly within the background.
Your workaround utilizing Date.now()
in replace()
works effectively for countdowns because it makes use of actual time as an alternative of body time. However such as you stated, it doesn’t assist a lot with tweens or something tied to the body fee.
For those who’re simply making an attempt to run one thing like a backend sync each few seconds, you might attempt utilizing setInterval
. It nonetheless will get throttled when the tab is inactive, however it normally runs higher than Cocos’s scheduler in these circumstances.
One other trick is to detect when the tab turns into lively once more after which deal with something that obtained missed whereas it was inactive.
Hope that helps a bit! Would love to listen to if somebody finds a greater answer too.
1 Like