kmcgrady / streamlit-autorefresh

An autorefresh component for Streamlit apps
MIT License
176 stars 16 forks source link

Add optional JS countdown timer #11

Open adneef opened 10 months ago

adneef commented 10 months ago

Overview:

Details:

  1. Add optional html/js timer functionality.
  2. Allow initialization option to turn countdown timer on - defaults to False.
  3. Allow initialization option to specify desired countdown timer text.

Proposed Solution:

<script>
    function startTimer(duration, display) {{
        var timer = duration, minutes, seconds;
        setInterval(function () {{
            minutes = parseInt(timer / 60, 10)
            seconds = parseInt(timer % 60, 10);

            minutes = minutes < 10 ? "0" + minutes : minutes;
            seconds = seconds < 10 ? "0" + seconds : seconds;

            display.textContent = minutes + ":" + seconds;

            if (--timer < 0) {{
                timer = duration;
            }}
        }}, 1000);
    }}

    window.onload = function () {{
        var duration = 60 * 5 ,
        display = document.querySelector('#time'),
        startTimer(duration, display);
    }};
</script>

<body style="font-family: 'Source Sans Pro', sans-serif; line-height: 24px; font-size: 1rem;">
    <div style="color: white">Next refresh in approx: <span id="time">5:00</span></div>
</body>

Notes: