Skip to content
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

The timer is suspended and displays the wrong time if another browser… #8540

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/base-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export interface ISurvey extends ITextProcessor, ISurveyErrorOwner {
maxOthersLength: number;
clearValueOnDisableItems: boolean;

maxTimeToFinishPage: number;

uploadFiles(
question: IQuestion,
name: string,
Expand Down
5 changes: 5 additions & 0 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ export class PageModel extends PanelModelBase implements IPage {
public set maxTimeToFinish(val: number) {
this.setPropertyValue("maxTimeToFinish", val);
}
public getMaxTimeToFinish(): number {
if(this.maxTimeToFinish !== 0) return this.maxTimeToFinish;
const res = !!this.survey ? this.survey.maxTimeToFinishPage : 0;
return res > 0 ? res : 0;
}
protected onNumChanged(value: number) { }
protected onVisibleChanged() {
if (this.isRandomizing) return;
Expand Down
19 changes: 7 additions & 12 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4415,7 +4415,7 @@ export class SurveyModel extends SurveyElementCore
private calcIsShowPrevButton(): boolean {
if (this.isFirstPage || !this.showPrevButton || this.state !== "running") return false;
var page = this.visiblePages[this.currentPageNo - 1];
return this.getPageMaxTimeToFinish(page) <= 0;
return page && page.getMaxTimeToFinish() <= 0;
}
private calcIsShowNextButton(): boolean {
return this.state === "running" && !this.isLastPage && !this.canBeCompletedByTrigger;
Expand Down Expand Up @@ -7183,7 +7183,7 @@ export class SurveyModel extends SurveyElementCore
if (!page) return { spent: 0, limit: 0 };
let pageSpent = page.timeSpent;
let surveySpent = this.timeSpent;
let pageLimitSec = this.getPageMaxTimeToFinish(page);
let pageLimitSec = page.getMaxTimeToFinish();
let surveyLimit = this.maxTimeToFinish;
if (this.showTimerPanelMode == "page") {
return { spent: pageSpent, limit: pageLimitSec };
Expand All @@ -7210,7 +7210,7 @@ export class SurveyModel extends SurveyElementCore
if (!page) return "";
var pageSpent = this.getDisplayTime(page.timeSpent);
var surveySpent = this.getDisplayTime(this.timeSpent);
var pageLimitSec = this.getPageMaxTimeToFinish(page);
var pageLimitSec = page.getMaxTimeToFinish();
var pageLimit = this.getDisplayTime(pageLimitSec);
var surveyLimit = this.getDisplayTime(this.maxTimeToFinish);
if (this.showTimerPanelMode == "page")
Expand Down Expand Up @@ -7243,7 +7243,7 @@ export class SurveyModel extends SurveyElementCore
pageSpent: string,
pageLimit: string
): string {
return this.getPageMaxTimeToFinish(page) > 0
return !!page && page.getMaxTimeToFinish() > 0
? this.getLocalizationFormatString("timerLimitPage", pageSpent, pageLimit)
: this.getLocalizationFormatString("timerSpentPage", pageSpent, pageLimit);
}
Expand Down Expand Up @@ -7353,19 +7353,14 @@ export class SurveyModel extends SurveyElementCore
public set maxTimeToFinishPage(val: number) {
this.setPropertyValue("maxTimeToFinishPage", val);
}
private getPageMaxTimeToFinish(page: PageModel) {
if (!page || page.maxTimeToFinish < 0) return 0;
return page.maxTimeToFinish > 0
? page.maxTimeToFinish
: this.maxTimeToFinishPage;
}
private doTimer(page: PageModel): void {
this.onTimer.fire(this, {});
if (this.maxTimeToFinish > 0 && this.maxTimeToFinish == this.timeSpent) {
if (this.maxTimeToFinish > 0 && this.maxTimeToFinish <= this.timeSpent) {
this.timeSpent = this.maxTimeToFinish;
this.completeLastPage();
}
if (page) {
var pageLimit = this.getPageMaxTimeToFinish(page);
var pageLimit = page.getMaxTimeToFinish();
if (pageLimit > 0 && pageLimit == page.timeSpent) {
if (this.isLastPage) {
this.completeLastPage();
Expand Down
7 changes: 1 addition & 6 deletions src/surveyTaskManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { ISurvey } from "./base-interfaces";
import { Base, EventBase } from "./base";
import { SurveyTimer } from "./surveytimer";
import { property } from "./jsonobject";
import { PageModel } from "./page";
import { SurveyModel } from "./survey";
import { CssClassBuilder } from "./utils/cssClassBuilder";

class SurveyTaskModel {
private timestamp: Date;
Expand All @@ -31,7 +26,7 @@ export class SurveyTaskManagerModel extends Base {
return task;
}

public waitAndExecute(action: any) {
public waitAndExecute(action: any): void {
if(!this.hasActiveTasks) {
action();
return;
Expand Down
14 changes: 9 additions & 5 deletions src/surveyTimerModel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ISurvey } from "./base-interfaces";
import { Base, EventBase } from "./base";
import { SurveyTimer } from "./surveytimer";
import { SurveyTimer, SurveyTimerEvent } from "./surveytimer";
import { property } from "./jsonobject";
import { PageModel } from "./page";
import { SurveyModel } from "./survey";
Expand Down Expand Up @@ -38,7 +38,7 @@ export class SurveyTimerModel extends Base {
this.survey.onCurrentPageChanged.add(() => {
this.update();
});
this.timerFunc = (): void => { this.doTimer(); };
this.timerFunc = (sender: SurveyTimer, options: SurveyTimerEvent): void => { this.doTimer(options.seconds); };
this.setIsRunning(true);
this.update();
SurveyTimer.instance.start(this.timerFunc);
Expand All @@ -58,12 +58,16 @@ export class SurveyTimerModel extends Base {
this.updateText();
this.updateProgress();
}
private doTimer(): void {
private doTimer(seconds: number): void {
var page = <PageModel>(<ISurvey><any>this.survey).currentPage;
if (page) {
page.timeSpent = page.timeSpent + 1;
const pageMaxTime = page.getMaxTimeToFinish();
if(pageMaxTime > 0 && pageMaxTime < page.timeSpent + seconds) {
seconds = pageMaxTime - page.timeSpent;
}
page.timeSpent = page.timeSpent + seconds;
}
this.spent = this.spent + 1;
this.spent = this.spent + seconds;
this.update();
if (this.onTimer) {
this.onTimer(page);
Expand Down
29 changes: 21 additions & 8 deletions src/surveytimer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from "./base";
import { EventBase } from "./base";

export var surveyTimerFunctions = {
setTimeout: (func: () => any): number => {
Expand All @@ -14,32 +14,39 @@ export var surveyTimerFunctions = {
} else {
return setTimeout(func, delay);
}
}
},
now(): number { return Date.now(); }
};

export interface SurveyTimerEvent {
seconds: number;
}

export class SurveyTimer {
private static instanceValue: SurveyTimer = null;
public static get instance() {
public static get instance(): SurveyTimer {
if (!SurveyTimer.instanceValue) {
SurveyTimer.instanceValue = new SurveyTimer();
}
return SurveyTimer.instanceValue;
}
private listenerCounter = 0;
private timerId = -1;
public onTimer: Event<() => any, SurveyTimer, any> = new Event<() => any, SurveyTimer, any>();
public start(func: () => any = null) {
private prevTimeInMs: number;
public onTimer: EventBase<SurveyTimer, SurveyTimerEvent> = new EventBase<SurveyTimer, SurveyTimerEvent>();
public start(func: (timer: SurveyTimer, options: SurveyTimerEvent) => void = null): void {
if (func) {
this.onTimer.add(func);
}
this.prevTimeInMs = surveyTimerFunctions.now();
if (this.timerId < 0) {
this.timerId = surveyTimerFunctions.setTimeout(() => {
this.doTimer();
});
}
this.listenerCounter++;
}
public stop(func: () => any = null) {
public stop(func: (timer: SurveyTimer, options: SurveyTimerEvent) => any = null): void {
if (func) {
this.onTimer.remove(func);
}
Expand All @@ -49,13 +56,19 @@ export class SurveyTimer {
this.timerId = -1;
}
}
public doTimer() {
public doTimer(): void {
if(this.onTimer.isEmpty || this.listenerCounter == 0) {
this.timerId = -1;
}
if (this.timerId < 0) return;
const newTimer = surveyTimerFunctions.now();
let seconds = Math.floor((newTimer - this.prevTimeInMs) / 1000);
this.prevTimeInMs = newTimer;
if(seconds < 0) {
seconds = 1;
}
const prevItem = this.timerId;
this.onTimer.fire(this, {});
this.onTimer.fire(this, { seconds: seconds });
//We have to check that we have the same timerId
//It could be changed during events execution and it will lead to double timer events
if(prevItem !== this.timerId) return;
Expand Down
Loading
Loading