lives: Stop existing timer when changing mind

This commit is contained in:
nemunaire 2023-03-03 13:15:36 +01:00
parent ce1d61b6c4
commit 5b2fddddc1
1 changed files with 14 additions and 3 deletions

View File

@ -309,6 +309,7 @@ func SurveyWSAdmin(c *gin.Context) {
go func(c chan WSMessage, sid int) {
var v WSMessage
var err error
var surveyTimer *time.Timer
for {
// Reset variable state
v.Corrected = false
@ -326,14 +327,24 @@ func SurveyWSAdmin(c *gin.Context) {
if survey, err := getSurvey(sid); err != nil {
log.Println("Unable to retrieve survey:", err)
} else {
// Skip any existing scheduled timer
if surveyTimer != nil {
if !surveyTimer.Stop() {
<-surveyTimer.C
}
surveyTimer = nil
}
survey.Direct = v.QuestionId
if v.Timer > 0 {
survey.Corrected = false
survey.Update()
go func(corrected bool) {
time.Sleep(time.Duration(OffsetQuestionTimer+v.Timer) * time.Millisecond)
// Save corrected state for the callback
corrected := v.Corrected
surveyTimer = time.AfterFunc(time.Duration(OffsetQuestionTimer+v.Timer)*time.Millisecond, func() {
surveyTimer = nil
if corrected {
survey.Corrected = v.Corrected
survey.Update()
@ -347,7 +358,7 @@ func SurveyWSAdmin(c *gin.Context) {
survey.WSWriteAll(WSMessage{Action: "pause"})
WSAdminWriteAll(WSMessage{Action: "pause", SurveyId: &survey.Id})
}
}(v.Corrected)
})
v.Corrected = false
} else {
survey.Corrected = v.Corrected