Skip to content

Commit 866d87c

Browse files
committed
fixed a bug where changing pages too quickly would show 2 pages at once
1 parent ca0e072 commit 866d87c

1 file changed

Lines changed: 42 additions & 21 deletions

File tree

public/js/script.js

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let wpmHistory = [];
1010
let currentCommands = commands;
1111
let restartCount = 0;
1212
let currentTestLine = 0;
13+
let pageTransition = false;
1314

1415
let accuracyStats = {
1516
correct: 0,
@@ -651,52 +652,70 @@ function changeTimeConfig(time) {
651652
}
652653

653654
function changePage(page) {
655+
if(pageTransition){
656+
return;
657+
}
654658
restartTest();
655659
let activePage = $(".page.active");
656660
$(".page").removeClass('active');
657661
$("#wordsInput").focusout();
658662
if (page == "test" || page == "") {
659-
$(".page.pageTest").addClass('active');
660-
swapElements(activePage, $(".page.pageTest"), 250, focusWords);
661-
history.pushState('/', null, '/');
663+
pageTransition = true;
664+
swapElements(activePage, $(".page.pageTest"), 250, () => {
665+
pageTransition = false;
666+
focusWords();
667+
$(".page.pageTest").addClass('active');
668+
history.pushState('/', null, '/');
669+
});
662670
showTestConfig();
663671
hideSignOutButton();
664672
restartCount = 0;
665673
restartTest();
666674
} else if (page == "about") {
667-
$(".page.pageAbout").addClass('active');
668-
swapElements(activePage, $(".page.pageAbout"), 250);
669-
history.pushState('about', null, 'about');
675+
pageTransition = true;
676+
swapElements(activePage, $(".page.pageAbout"), 250, ()=>{
677+
pageTransition = false;
678+
history.pushState('about', null, 'about');
679+
$(".page.pageAbout").addClass('active');
680+
});
670681
hideTestConfig();
671682
hideSignOutButton();
672683
} else if (page == "settings") {
673-
updateSettingsPage()
674-
$(".page.pageSettings").addClass('active');
675-
swapElements(activePage, $(".page.pageSettings"), 250);
676-
history.pushState('settings', null, 'settings');
684+
pageTransition = true;
685+
swapElements(activePage, $(".page.pageSettings"), 250, ()=>{
686+
pageTransition = false;
687+
history.pushState('settings', null, 'settings');
688+
$(".page.pageSettings").addClass('active');
689+
});
690+
updateSettingsPage();
677691
hideTestConfig();
678692
hideSignOutButton();
679693
} else if (page == "account") {
680694
if (!firebase.auth().currentUser) {
681695
changePage("login");
682696
} else {
683-
$(".page.pageAccount").addClass('active');
684-
swapElements(activePage, $(".page.pageAccount"), 250);
697+
pageTransition = true;
698+
swapElements(activePage, $(".page.pageAccount"), 250, () =>{
699+
pageTransition = false;
700+
history.pushState('account', null, 'account');
701+
$(".page.pageAccount").addClass('active');
702+
});
685703
refreshAccountPage();
686-
history.pushState('account', null, 'account');
687704
hideTestConfig();
688705
showSignOutButton();
689706
}
690707
} else if (page == "login") {
691708
if (firebase.auth().currentUser != null) {
692709
changePage('account');
693710
} else {
694-
$(".page.pageLogin").addClass('active');
695-
swapElements(activePage, $(".page.pageLogin"), 250);
696-
history.pushState('login', null, 'login');
711+
pageTransition = true;
712+
swapElements(activePage, $(".page.pageLogin"), 250, ()=>{
713+
pageTransition = false;
714+
history.pushState('login', null, 'login');
715+
$(".page.pageLogin").addClass('active');
716+
});
697717
hideTestConfig();
698718
hideSignOutButton();
699-
700719
}
701720
}
702721
}
@@ -763,14 +782,14 @@ function swapElements(el1, el2, totalDuration, callback = function() { return; }
763782
) {
764783
//one of them is hidden and the other is visible
765784
if (el1.hasClass("hidden")) {
785+
callback();
766786
return false;
767787
}
768-
769-
$(el1).stop(true, true).removeClass('hidden').css('opacity', 1).animate({
788+
$(el1).removeClass('hidden').css('opacity', 1).animate({
770789
opacity: 0
771790
}, totalDuration / 2, () => {
772791
$(el1).addClass('hidden');
773-
$(el2).stop(true, true).removeClass('hidden').css('opacity', 0).animate({
792+
$(el2).removeClass('hidden').css('opacity', 0).animate({
774793
opacity: 1
775794
}, totalDuration / 2, () => {
776795
callback();
@@ -779,11 +798,13 @@ function swapElements(el1, el2, totalDuration, callback = function() { return; }
779798

780799
} else if (el1.hasClass('hidden') && el2.hasClass('hidden')) {
781800
//both are hidden, only fade in the second
782-
$(el2).stop(true, true).removeClass('hidden').css('opacity', 0).animate({
801+
$(el2).removeClass('hidden').css('opacity', 0).animate({
783802
opacity: 1
784803
}, totalDuration, () => {
785804
callback();
786805
});
806+
}else{
807+
callback();
787808
}
788809

789810
}

0 commit comments

Comments
 (0)