Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
780 views
in Technique[技术] by (71.8m points)

python 3.x - Creating a countdown timer with Flask, SocketIO

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...