Change Slider with Sessions

What I was tried to do is changing slider to the next slide after I navigate to a new page. Is there a way handle with this instead of working on code given below?

import { session } from 'wix-storage';
import wixLocation from 'wix-location';
let previousSlideIndex;
let currentIndex;

$w.onReady(function () {

    previousSlideIndex = session.getItem("slide");
    currentIndex = Number(previousSlideIndex) + 1
    $w("#mySlideshow").changeSlide(currentIndex);

    wixLocation.onChange(() => {
        currentIndex = $w("#mySlideshow").currentIndex;
        session.setItem("slide", currentIndex);
    });

});

It changes the slider to the second or third slide on live. How can I fix it?

Hi there :raised_hand_with_fingers_splayed:

The onChange( ) event handler on the location APIs doesn’t get triggered when you navigate from page to page, it’s mainly used with applications installed, such as Wix Stores, Wix Bookings, etc.

If you elaborate more on what exactly you want to do - since I didn’t get it - I might be able to suggest another way to achieve your goal here.

What I was tried to do is changing slider to the next slide after I navigate to a new page. I mean, when someone navigate between pages, slider shouldnt play from the beginning, instead, it should play from where it left.

@velogenius Is the slider shown on all pages?

@ahmadnasriya yes, it was attached onto the header.

@ahmadnasriya Updated code is here.

import { session } from 'wix-storage';
import wixLocation from 'wix-location';

let currentIndex;

$w.onReady(function () {
    setCurrentSlide();
    $w("#mySlideshow").onChange(setSlideSession);

    // wixLocation.onChange(setSlideSession);
});

function setCurrentSlide() {
    let maxIx = $w("#mySlideshow").slides.length - 1;
    let temp = session.getItem("slide");
    if (temp !== undefined) {
        currentIndex = parseInt(temp) + 1;
    } else {
        currentIndex = 0;
    }
    if (currentIndex > maxIx) {
        currentIndex = 0;
    }
    $w("#mySlideshow").play();
    $w("#mySlideshow").changeSlide(currentIndex);
}

function setSlideSession() {
    
    let ci = $w("#mySlideshow").currentIndex;
    session.setItem("slide", ci.toString());
    
}

But when I navigate between pages, slideshow moves to the previous slides. What can be done?