I am trying to build a countdown timer using Flask and SocketIO, however I'm struggling with how form handling works because I created a stop timer button but it wont stop. The timer it self works, however the stop doesn't work.
<form method="post" action="/countdownTimer" name="timer" id="timer" target="dummyframe">
<input type="text" name="hour">
<input type="text" name="minute">
<input type="text" name="seconds">
<input type="submit" value="timer_start" name="timer_start"/>
<input type="submit" value="timer_stop" name="timer_stop"/>
</form>
def convert2(seconds):
seconds = seconds % (24 * 3600)
seconds %= 36000
minutes = seconds // 60
seconds %= 60
return "%02d:%02d" % (minutes, seconds)
@app.route('/countdownTimer', methods=['GET', 'POST'])
def countdownTimer():
time = -1
if request.form.get('timer_start') == "timer_start":
startTimer = True
hour = int(request.form['hour'])
minute = int(request.form['minute'])
seconds = int(request.form['seconds'])
time = ((hour*60)*60) + (minute*60) + seconds
while int(time) > 0:
if request.form.get('timer_stop') == "timer_stop":
print("Stopped")
time = -1
socketio.sleep(1)
time = time-1
socketio.emit('Timer', convert2(int(time)))
print(convert2(int(time)))
return str(time)
Thank you
question from:
https://stackoverflow.com/questions/65895711/creating-a-countdown-timer-with-flask-socketio 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…