DMOJ / online-judge

A modern open-source online judge and contest platform system.
https://dmoj.ca
GNU Affero General Public License v3.0
958 stars 367 forks source link

Contest Timer Wrong #1733

Open slightlyskepticalpotat opened 3 years ago

slightlyskepticalpotat commented 3 years ago

The contest timer is sometimes drastically wrong, but returns to normal after a refresh. I think this is due to chromium throttling javascript.


Platform: Chromium 91.0.4472.114 (64-bit) on Ubuntu 20.04.02

Steps to reproduce:

The timer on the focused tab should be correct, and the timer on the other tab should be an hour out of date. After refreshing, the delayed timer should go back to normal.

slightlyskepticalpotat commented 1 year ago

Looks like it's because of setInterval being wack here https://github.com/DMOJ/online-judge/blob/3f5212fdcbfce92fe28f8d43faf2e9b31a4afc00/resources/common.js#L181

slightlyskepticalpotat commented 1 year ago

What if I just changed it into

var timer = setInterval(function () {
    if (!document.hidden){ // addition to prevent it updating when tab not active
        var time = Math.round(initial - (Date.now() - start) / 1000);
        if (time <= 0) {
            clearInterval(timer);
            setTimeout(function() {
                window.location.reload();
            }, 2000);
        }
        var d = Math.floor(time / 86400);
        var h = Math.floor(time % 86400 / 3600);
        var m = Math.floor(time % 3600 / 60);
        var s = time % 60;
        if (d > 0)
            label.text(npgettext('time format with day', '%d day %h:%m:%s', '%d days %h:%m:%s', d)
                .replace('%d', d).replace('%h', format(h)).replace('%m', format(m)).replace('%s', format(s)));
        else
            label.text(pgettext('time format without day', '%h:%m:%s')
                .replace('%h', format(h)).replace('%m', format(m)).replace('%s', format(s)));
    }}, 1000);
slightlyskepticalpotat commented 1 year ago

image It looks cursed but it should work. Will PR after I test.