0
I have the following code inside my script:
(Function() { 'use Strict';
let $leftside_div = document.querySelector('.leftside-div');
let $centerside_div = document.querySelector('.centerside-div');
let $rightside_div = document.querySelector('.rightside-div');
$leftside_div.addEventListener('click', handleIncorrectAnswer, false);
$centerside_div.addEventListener('click', handleCorrectAnswer, false);
$rightside_div.addEventListener('click', handleIncorrectAnswer2, false);
const redirectToCorrectPage = () => {
window.location.href = "right-answer.html";
}
const redirectToIncorrectPage = () => { window.location.href = "Wrong-Answer.html"; }
function handleCorrectAnswer() {
$centerside_div.classList.replace('centerside-div', 'centerside-div-js' );
$rightside_div.classList.replace('rightside-div', 'rightside-div-js-no-display');
$leftside_div.classList.replace('leftside-div', 'leftside-div-js-no-display');
setTimeout(redirectToCorrectPage, 1000);
}
function handleIncorrectAnswer() {
$centerside_div.classList.replace('centerside-div', 'centerside-div-js' );
$rightside_div.classList.replace('rightside-div', 'rightside-div-js-no-display');
$leftside_div.classList.replace('leftside-div', 'leftside-div-js-no-display');
setTimeout(redirectToIncorrectPage, 2000);
}
function handleIncorrectAnswer2() {
$centerside_div.classList.replace('centerside-div', 'centerside-div-js' );
$rightside_div.classList.replace('rightside-div', 'rightside-div-js-no-display');
$leftside_div.classList.replace('leftside-div', 'leftside-div-js-no-display');
setTimeout(redirectToIncorrectPage, 2000);
}
})()
There are 3 setTimeout running, in which the handleCorrectAnswer function is leading to a specific page, and the handleIncorrectAnswer and handleIncorrectAnswer2 functions are leading to another single page.
I already tried to put the script on the redirect page, however, as there are two redirect pages, I could not create the logic of how it would go back to the page I wish it would come back.
My difficulty I am finding is the following: I would like, after performing a setTimeout and entering the other page, to wait and perform another Timeout to redirect to another page, all in the same function. Is there such a possibility?
if you are changing pages... when changing page your js context is lost, what can you do is try to flag the url with some standard string after # or ? , not to affect the destination location, or even by cookies, and on the destination page, pick up what is in the url after # or ? or in the cookie to verify and apply the next setTimeout if applicable...
– anon
ah less it is clear that you are using the history API, which does not seem to be the case...
– anon