Page 1 of 1

Issue with javascript code

Posted: 19 Jan 2025, 05:48
by Faust
Hi. I made some adjustments on my website  and added some animation numbers. However everything works fine when first time the site is visited, but when clicking on any page on the website and coming back again on home page, the numbers are shown 0 for some reason. Any ideas what adjustments should I make?
Code
document.addEventListener('DOMContentLoaded', () => {
    const counters = document.querySelectorAll('.stat-item h2');
    const duration = 2000; // Total animation duration in milliseconds

    const updateCount = (counter) => {
        const target = +counter.getAttribute('data-target');
        const count = +counter.innerText;

        console.log(`Updating counter from ${count} to ${target}`); // Debugging message

        // Calculate increment with a minimum step for small numbers
        const increment = target / (duration / 50);
        const step = target < 20 ? 0.1 : increment; // Use smaller step for small numbers

        if (count < target) {
            counter.innerText = Math.min(target, Math.ceil(count + step));
            setTimeout(() => updateCount(counter), 50);
        } else {
            counter.innerText = target;
            console.log(`Counter reached target ${target}`); // Debugging message
            localStorage.setItem(counter.getAttribute('data-target'), target); // Store the final value
        }
    };

    const observer = new IntersectionObserver(entries => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                const counter = entry.target;
                const storedValue = localStorage.getItem(counter.getAttribute('data-target'));
                if (storedValue) {
                    counter.innerText = storedValue; // Set the stored value
                } else {
                    console.log(`Starting animation for counter with target ${counter.getAttribute('data-target')}`); // Debugging message
                    counter.innerText = '0'; // Ensure the counter starts from 0
                    updateCount(counter);
                }
                observer.unobserve(counter); // Stop observing once the animation starts or value is set
            }
        });
    }, { threshold: 1 });

    counters.forEach(counter => {
        observer.observe(counter);
    });
});

Link:

https://happyfeelings.co.uk

Re: Issue with javascript code

Posted: 19 Jan 2025, 09:29
by mjau-mjau
Faust wrote:Any ideas what adjustments should I make?
Because X3 is a SPA (singe-page application) that loads and injects pages dynamically on navigation, your code would only trigger once on the first page load. If you want to trigger something on every page load, you need to use an X3 function x3_load_page(). The question is, where is your code supposed to trigger? Only for a certain page? Or for all pages? Where did you add the code?

For instance, instead of 'DOMContentLoaded', you would use this to trigger javascript on every page:
Code
function x3_load_page(){
  // put your code here
  console.log('Runs after each X3 ajax page load');
}
But implementation depends slightly on when and where you want to code to trigger.

Re: Issue with javascript code

Posted: 01 Feb 2025, 16:03
by Faust
mjau-mjau wrote:
Faust wrote:Any ideas what adjustments should I make?
Because X3 is a SPA (singe-page application) that loads and injects pages dynamically on navigation, your code would only trigger once on the first page load. If you want to trigger something on every page load, you need to use an X3 function x3_load_page(). The question is, where is your code supposed to trigger? Only for a certain page? Or for all pages? Where did you add the code?

For instance, instead of 'DOMContentLoaded', you would use this to trigger javascript on every page:
Code
function x3_load_page(){
  // put your code here
  console.log('Runs after each X3 ajax page load');
}
But implementation depends slightly on when and where you want to code to trigger.
Thanks for reply. I have added the code on home page. You can check it here:

https://happyfeelings.co.uk/

So, I should add that code on all pages? 

Re: Issue with javascript code

Posted: 01 Feb 2025, 19:39
by mjau-mjau
Faust wrote: Thanks for reply. I have added the code on home page. You can check it here:

https://happyfeelings.co.uk/
What am I supposed to be looking for?
Faust wrote:So, I should add that code on all pages? 
No. The point of that function as emphasized in the name "x3_load_page()", is that it triggers for every page load. It already triggers every time a page loads.

Re: Issue with javascript code

Posted: 04 Feb 2025, 15:09
by Faust
mjau-mjau wrote:
Faust wrote: Thanks for reply. I have added the code on home page. You can check it here:

https://happyfeelings.co.uk/
What am I supposed to be looking for?
Faust wrote:So, I should add that code on all pages? 
No. The point of that function as emphasized in the name "x3_load_page()", is that it triggers for every page load. It already triggers every time a page loads.
The animations of numbers works only when first time visiting the website. When clicking on other pages and come back to the homepage, the numbers are set to 0 and not moving at all. 

I am asking if there is any way to make it work. 

Re: Issue with javascript code

Posted: 05 Feb 2025, 20:58
by mjau-mjau
From my first response to this, I already gave a reason and solution, which you have not followed:
mjau-mjau wrote:
Faust wrote:Any ideas what adjustments should I make?
Because X3 is a SPA (singe-page application) that loads and injects pages dynamically on navigation, your code would only trigger once on the first page load. If you want to trigger something on every page load, you need to use an X3 function x3_load_page(). The question is, where is your code supposed to trigger? Only for a certain page? Or for all pages? Where did you add the code?

For instance, instead of 'DOMContentLoaded', you would use this to trigger javascript on every page:
Code
function x3_load_page(){
  // put your code here
  console.log('Runs after each X3 ajax page load');
}
But implementation depends slightly on when and where you want to code to trigger.
Your global Javascript starts like this:
Code
document.addEventListener('DOMContentLoaded', () => {
Yet I told you to put it inside x3_load_page function instead:
Code
function x3_load_page(){
  // put your code here
  console.log('Runs after each X3 ajax page load');
}
It doesn't seem like you followed my suggestion or done anything since the original post.

Another solution is to instead add your code inside the index PAGE settings > page, scroll down to "Advanced" > Page Javascript. When added there, it will only and always trigger when the page loads, and you can add the code without the listener "document.addEventListener('DOMContentLoaded', () => {" ...

Or send me panel login by email and I will fix it for you.

Re: Issue with javascript code

Posted: 07 Feb 2025, 11:37
by Faust
Thanks for response, I have sent you a PM. Tried to add it to the advanced javascript field, but still no changes.

Re: Issue with javascript code

Posted: 07 Feb 2025, 19:22
by mjau-mjau
Works now. See code in Settings > Custom > Custom Javascript.

I see you have Cloudflare enabled. Be careful with this, because it means pages get cached, and you need to clear the Cloudflare cache every time you make a change to a page. This can be confusing, and could lead to your changes not seeming to come through. I would disable it if I was you, it's not worth it.

Re: Issue with javascript code

Posted: 08 Feb 2025, 15:50
by Faust
mjau-mjau wrote: Works now. See code in Settings > Custom > Custom Javascript.

I see you have Cloudflare enabled. Be careful with this, because it means pages get cached, and you need to clear the Cloudflare cache every time you make a change to a page. This can be confusing, and could lead to your changes not seeming to come through. I would disable it if I was you, it's not worth it.
Thanks Karl. I've disabled on my both sites.