Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

when a certain number of clicks has been reached, then change the image

I want to build a cookie clicker for a project. With each click on an image, the clicks are counted and displayed. Now I want the image to change automatically after 10000 clicks, i.e. a new image replaces the old one and the current number of clicks is saved in local storage.

It doesn’t quite work for me. Only when I save the page and then reload it is the picture changed and not automatically.

    if (clickercount >= 10000) {
        $(".clickImg").attr("src", "new_image.png")
    } else {
    }

    $('#saveButton').click(function () {
        saveCurrentStatus()
    });

    $('#resetButton').click(function () {
        localStorage.clear();
        location.reload(true)
    });

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Judging from the information above, it seems like you are only executing the code once. And that happens only when you load the page. You could use an event listener to listen for clicks on your cookie, and run the piece of code in the event listener.

$('.clickImg').click(function () {
    if (clickercount >= 10000) {
        $(".clickImg").attr("src", "new_image.png")
    } else {
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading