Skip to content

Commit

Permalink
Fixed #7507 - Timer doesn't stop at 0 when a survey has validation er…
Browse files Browse the repository at this point in the history
…rors
  • Loading branch information
tsv2013 committed Dec 27, 2023
1 parent 76107ef commit 3c7e4f8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/surveyTimerModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ export class SurveyTimerModel extends Base {
@property() clockMinorText: string;
@property({ defaultValue: 0 }) spent: number;
public get survey(): ISurveyTimerText { return <any>this.surveyValue; }
public onCreating(): void {}
public onCreating(): void { }
private timerFunc: any = null;
public start(): void {
if(!this.survey) return;
if (!this.survey) return;
if (this.isRunning || this.isDesignMode) return;
this.survey.onCurrentPageChanged.add(() => {
this.update();
Expand Down Expand Up @@ -65,23 +65,26 @@ export class SurveyTimerModel extends Base {
}
this.spent = this.spent + 1;
this.update();
if(this.onTimer) {
if (this.onTimer) {
this.onTimer(page);
}
}
private updateProgress() {
let { spent, limit } = this.survey.timerInfo;
if(!limit) {
if (!limit) {
this.progress = undefined;
} else {
if(spent == 0) {
if (spent == 0) {
this.progress = 0;
setTimeout(() => {
this.progress = Math.floor((spent + 1)/limit * 100) / 100;
this.progress = Math.floor((spent + 1) / limit * 100) / 100;
}, 0);
}
else if(spent !== limit) {
this.progress = Math.floor((spent + 1)/limit * 100) / 100;
else if (spent <= limit) {
this.progress = Math.floor((spent + 1) / limit * 100) / 100;
}
if (this.progress > 1) {
this.progress = undefined;
}
}
}
Expand Down
35 changes: 34 additions & 1 deletion tests/surveytimertests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,37 @@ QUnit.test("Check timer when limits are not specified", function(assert) {
assert.strictEqual(timerModel.clockMajorText, "0:01");
assert.strictEqual(timerModel.clockMinorText, undefined);
survey.stopTimer();
});
});

QUnit.test("Progress shouldn't be more than 1", function (assert) {
const createSurvey = (maxTimeToFinish: number, maxTimeToFinishPage: number): SurveyModel => {
var survey = new SurveyModel();
survey.maxTimeToFinish = maxTimeToFinish;
survey.maxTimeToFinishPage = maxTimeToFinishPage;
survey.addNewPage("p1");
survey.pages[0].addNewQuestion("text");
return survey;
};
var survey = createSurvey(3, 3);
const timerModel = survey.timerModel;
survey.startTimer();
assert.equal(survey.timerInfo.limit, 3);
assert.equal(survey.timerInfo.spent, 0, "initial spent");
assert.equal(timerModel.progress, 0, "initial progress");
doTimer(1);
assert.equal(survey.timerInfo.limit, 3);
assert.equal(survey.timerInfo.spent, 1, "spent 1");
assert.equal(timerModel.progress, 0.66, "progress 1");
doTimer(1);
assert.equal(survey.timerInfo.limit, 3);
assert.equal(survey.timerInfo.spent, 2, "spent 2");
assert.equal(timerModel.progress, 1, "progress 2");
doTimer(1);
assert.equal(survey.timerInfo.limit, 3);
assert.equal(survey.timerInfo.spent, 3, "spent 3");
assert.equal(timerModel.progress, undefined, "progress 3");
doTimer(1);
assert.equal(survey.timerInfo.limit, 3);
assert.equal(survey.timerInfo.spent, 3, "spent 4");
assert.equal(timerModel.progress, undefined, "progress 4");
});

0 comments on commit 3c7e4f8

Please sign in to comment.