I write my countdown timer works successfully but when i page refresh countdown timer resets. Countdown timer doesn't start if i don't open page and not sync work all clients.
This is my server-side python flask code :
from flask import Flask,render_template,render_template_string
from flask_socketio import SocketIO, send, emit
app = Flask(__name__)
app.config['SECRET KEY'] = 'random'
socketio = SocketIO(app)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('test_timer')
def Timer(seconds=3600):
def hms(seconds): # hour minute second function
h = seconds // 3600
m = seconds % 3600 // 60
s = seconds % 3600 % 60
return '{:02d}:{:02d}:{:02d}'.format(h, m, s)
for i in range(seconds):
emit('test_timer_reply',hms(seconds-i),broadcast=True)
socketio.sleep(1)
if __name__ == '__main__':
socketio.run(app,debug=True)
This is my client-side javascript code
<!DOCTYPE html>
<html>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
var socket = io();
socket.emit('test_timer')
socket.on('test_timer_reply', function(receiving_data) {
console.log(receiving_data);
});
</script>
</body>
</html>
How do I get the timer to work before I open the page?
How do I ensure that the timer is not reset when I refresh the page?
How do I the timer to work synchronously(countdown time it will be same all clients) on all clients?