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

How to stop the interval after changing all three messages?

I need to stop the interval after changing all messages but I can’t. Can you help me? Thanks.

<script>
$(document).ready(function(){
    var textos = ["Hi", "Fine?", "0"];
    var atual = 0;
    $('#display').text(textos[atual++]);
    setInterval(function() {
        $('#display').fadeOut(function() {
            if (atual >= textos.length) atual = 0;
            $('#display').text(textos[atual++]).fadeIn();
        });
    }, 10);
});
</script>

>Solution :

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

use for loop? or put this in a var and cleat when needed

<script>
    $(document).ready(function(){
        var textos = ["Hi", "Fine?", "0"];
        var atual = 0;
      // here
        var intervalId;
        $('#display').text(textos[atual++]);
        intervalId = setInterval(function() {
            $('#display').fadeOut(function() {
                if (atual >= textos.length) {
            // clear
                    clearInterval(intervalId);
                } else {
                    $('#display').text(textos[atual++]).fadeIn();
                }
            });
        }, 1000);
    });
</script>

to keep last one try this

$(document).ready(function(){
    var textos = ["Hi", "Fine?", "0"];
    var atual = 0;
    var intervalId;

    $('#display').text(textos[atual++]);
    intervalId = setInterval(function() {
        if (atual >= textos.length - 1) {
            clearInterval(intervalId);
        } else {
            $('#display').fadeOut(function() {
                $('#display').text(textos[atual++]).fadeIn();
            });
        }
    }, 1000);
});
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