Reputation: 315
Lets say I am building a pub-quiz app with Django Channels (Websockets). There are 10 questions and each question has a time limit and a button to add extra 5 seconds.
I know a possibility would be to haver the timer client-side, but I need it to be server-side to avoid tampering with the Js causing problems since there are many players involved.
How can I create a timer in Django, server-side? Once the timer is up, it would need to call a function to fetch the next question. I would know how to do it with a while loop:
While True:
if datetime.now() == question_end_time:
fetch_next_question()
But from what I have read this does not seem to be a good idea due to blocking the thread. Is there any other way how to check every second if the time for the question is up and is so, call a function?
Thanks in advance.
Upvotes: 2
Views: 995
Reputation: 647
What about going the other way with it, and instead of checking every second you instead set something to happen at the end of a timer.
Use javascript to create a timer, and when the timer ends the javascript will call the django url, with ajax.
That way you aren't monitoring every second.
Upvotes: 2