Global continuous 3 minutes countdown

This account is too new to comment, but are you saying that you want a global 3 minute refresh so that ALL pages on the website (or ones you select) refresh for every user at the exact same time?

I’m thinking the best way to achieve this is to use a context_processor. In Django, you can add a context_processors.py that define’s an extra piece of context that gets added to every template. Inside of context_processors.py you can do something like:

from datetime import datetime, timedelta

def get_refresh_time():
    now = datetime.now()
    hour = now.hour
    minute = now.minute
    second = now.second
    if minute%3 == 0:
        new_datetime = datetime(now.year, now.month, now.day, now.hour, 
now.minute) + timedelta(minutes=3)
    elif minute%3 == 1:
        new_datetime = datetime(now.year, now.month, now.day, now.hour, 
now.minute) + timedelta(minutes=2)
    elif minute%3 == 2:
        new_datetime = datetime(now.year, now.month, now.day, now.hour, 
now.minute) + timedelta(minutes=1)
    
    additional_context = {'refresh_time': new_datetime}
    return additional_context

This would make {{ refresh_time }} accessible in any HTML file you have. Then, you can add it to a jQuery function to refresh the page AT that time.

Be sure to add that context_processor to the right spot in settings.TEMPLATES

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top