How can I prevent my values from restarting when I receive a post request?

guts716

The overall goal is to have a python program that upon receiving its first post request, it will render a html template that uses JavaScript to start a stopwatch. When the python program receives its second post request, I would like to be able to grab the values of the stopwatch at that current moment in time.

I was going to use the browsers local storage but quickly realized that would not be an option considering I will be deploying to heroku. My current implementation fails because each time I return the html template(or I send a new post request), the values get reset and the post request is never sent back to my python script(127.0.0.1:5000/test).

How can I start the stopwatch upon the first post request to my python script, and then grab those values without restarting it upon the second post request to my python script?

Python program - I am including very little of this file because I do not think it is necessary. Please let me know if I should include more.

@app.route('/test', methods=['POST'])
def dashboard():
    return render_template('dashboard.html')

dashboard.html

<!DOCTYPE html>
<html>
<body>
    <div id="stopwatch-container">
        <div id="stopwatch">00:00:00</div>
    </div>
    <script>
        var stopwatch = document.getElementById("stopwatch");
        var startBtn = document.getElementById("start-btn");
        var timeoutId = null;
        var ms = 0;
        var sec = 0;
        var min = 0;
        if(ms > 0){
           postHook(sec); 
        } else {
           start(); 
        }
        
        function postHook(sec){
            object = { 
                stopwatchValues: sec 
            }
            fetch("https://127.0.0.1:5000/test", { method: 'POST',body: JSON.stringify(object) })
        }

        /* function to start stopwatch */
        function start(count) {
            timeoutId = setTimeout(function() {
                ms = parseInt(ms);
                sec = parseInt(sec);
                min = parseInt(min);
                console.log(sec)
 
                ms++;
 
                if (ms == 100) {
                    sec = sec + 1;
                    ms = 0;
                }
                if (sec == 60) {
                    min = min + 1;
                    sec = 0;
                }
                if (ms < 10) {
                    ms = '0' + ms;
                }
                if (sec < 10) {
                    sec = '0' + sec;
                }
                if (min < 10) {
                    min = '0' + min;
                }
                if(count == null){
                    count = 69;
                }
                stopwatch.innerHTML = min + ':' + sec + ':' + ms + ' | ' + count;
 
                // calling start() function recursivly to continue stopwatch
                start();
 
            }, 10); // setTimeout delay time 10 milliseconds
        }
    </script>
</body>
</html>

code

This is actually not as complicated as you think; the implementation would go along the lines of...

  1. Create 2 variables in your server (make sure it's declared at the top level and not inside your controller function; feel free to rename): first_post (defaulting to False) and first_post_time, which will hold a timestamp of when the server received the first request.
  2. On a POST request, check if first_post is True. If not, set it to True and you know that it's the first post request.
  3. (If this is the first request) on your server, generate a (current) timestamp and assign it to first_post_time, then render the template as usual.
  4. On any subsequent POST request, take the current timestamp and subtract it from first_post_time. This is the current time displayed on the first user's stopwatch. Render a template with the value or do whatever you want with it.

Note: this implementation will have some latency on other client's devices, as the culminative HTTP requests may take a while to process. If you need as best accuracy as possible, look into realtime client-server-client or p2p solutions like websockets or WebRTC, respectively.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I edit on my server files without restarting nodejs when i want to see the changes?

How do I get the POST values from this request?

How can I prevent my HTTP Get request from being cached in Swift?

How can I prevent SKAction sequence restarting after decoding?

How can I prevent the debugger from entering my method when Exception is thrown?

How can I prevent all of my flex-items from resizing when I resize my window?

How can I prevent a Lazy Spool from happening in my query?

Why can't I receive the request query in for my delete request?

How can I prevent my python program from loading when using pyngrok?

How can I prevent my text from being pushed up when I add more content?

How can I stop Mac OS X overriding my hostname when I receive a DHCP request on Snow Leopard?

How can I prevent systemd from freezing my system when Flash dies?

How can I prevent Windows from disabling Aero when I use my secondary monitor?

How can I prevent my form from vertically stretching when "live"?

Runscope – How can I send a JSON body with my POST request?

How do I prevent my cd ripper from overwriting Unknown Album when metadata can't be found

How can i make a POST request in my local network when the origin is http and the target url is https?

How can I send a post request to another API from my own application

How can I receive the values from a form in the parent component?

How can I send an error from my server to the client after a post request?

How can I prevent my image from opening when long click

How can I prevent this from running on my screen reader when input is empty?

How do i can check if my post string contains a symbol and prevent from proceeding

How can I prevent my array from being overwritten when setting the state?

How can I prevent my React app from crashing when I scroll to a non-indexed value in my array?

How can I prevent my text from showing under my header when scrolling?

How can I deal with the response to a POST request sent to my website from an external webserver?

How can I prevent a white border from appearing on the first half of my webpage when zooming in?

How can I bring in updated google spreadsheet values into my python script without stopping and restarting it?