Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567067
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:47:56+00:00 2026-06-11T17:47:56+00:00

I have a jQuery function that uses setInterval() to slowly type out a phrase–like

  • 0

I have a jQuery function that uses setInterval() to slowly type out a phrase–like with the phrase “string one”, each character would be printed to the screen after a set number of milliseconds.

The problem is that in my code, I have two calls to the function one right after the other, and they’re overlapping because it appears that setInterval() doesn’t stop the normal flow of code. It’s hard to explain, here’s my jsFiddle that shows what happens.

Here’s my function:

function type(text) {
    if(intv == null) {
        var textArray = text.split("");
        var length = textArray.length - 1;
        var i = 0;

        $("#div").append('<p class="text' + n + '"></p>' + "\n"); // creates a paragraph to print every character to (n is a global var)

        intv = setInterval(function() {
            if(i <= length) {
                var a = textArray[i];
                $('#div .text' + n).text($('#div .text' + n).text() + a); // appends the character to the paragraph created above
                i++;
            }
            else {
                alert("stopping interval..."); // just lets me know when the interval is stopping
                clearInterval(intv);
                intv = null;
                n++;
            }
        }, 60);
    }
    else {
        alert("the interval is not null"); // lets me know if the previous setInterval has stopped firing
    }
}

And here’s how I’m calling it:

type("string one");
type("string two");

The problem is that both “string one” and “string two” end up firing at the same time, and either start infinitely looping or only the first one works.

I really don’t know if there’s a way to fix this, and my searches have yielded nothing. I don’t know if this is a common problem or not. Is there a way that I can keep the second string from firing until the first one is done?

Thanks ahead of time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:47:57+00:00Added an answer on June 11, 2026 at 5:47 pm

    Have a look here: http://jsfiddle.net/ymxu5/12/

    First, set up a function queue that will keep track of where we are:

    function Queue(){
        this._fns = [];
        this._args = [];
        this._running = false;
        var that = this;
    
        this.add = function(fn){
            console.log(fn.name + " added");
            that._fns.push(fn);
            var args = Array.prototype.slice.call(arguments, 1);
            that._args.push(args);
    
            if (!that._running)
                that.next();
    
            return that;
        },
        this.next = function(){        
            if (that._fns.length == 0){
                that._running = false;
            }
            else{
                that._running = true;
                that._fns.shift().apply(that, that._args.shift());
            }
        }
    };
    

    You’ll then need to adapt your methods to use queue.next();. I’ve set up the Queue object to call each function, changing the context of this to the Queue object, which is why var queue = this; is used.

    function type(text){
        var queue = this;
        var $con = $("<p>").appendTo("#div");
    
        var typeText = function(con, txt, cursor){
            if (cursor < txt.length){
                con.text(con.text() + txt[cursor]);
                setTimeout(function(){
                    typeText(con, txt, cursor + 1);
                }, 60);
            }
            else{
                setTimeout(function(){
                    queue.next();
                }, 250);
            }
        };
        typeText($con, text.split(""), 0);
    }
    
    function flicker(sel){
        var queue = this;
    
        // note that this will cause issues if "sel" selects multiple things
        // because it will create multiple callbacks with jQuery, and `next` will
        // be called once for each item in "sel"
        $(sel).fadeOut("fast", function(){
            $(this).fadeIn("fast", function(){
                queue.next();
            });
        });
    }
    

    You’ll want to have a Queue object accessible throughout your scope. I made it global here:

    var q = new Queue();
    var senNum = 0;
    
    $(function(){    
        $("#button").click(function(){
            q.add(type, "Sentence " + (++senNum)).add(flicker, "p:nth-child("+senNum+")");
        });        
    });
    

    I also took the liberty of making the add method chainable, so you can use it like above.
    ​

    here’s an example of using a few methods to create a console-like interface:

    http://jsfiddle.net/ymxu5/15/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jQuery setInterval function named timerIncrement that times out (stops incrementing the
I have a jQuery file that uses the data function to request data from
I have a page that uses the jquery function: $(document).ready(function() { and I have
I have a jQuery function that uses ajax to get data, then displays it,
I have simple AJAX function that uses jQuery to return an array of 300
I have a jquery dialog that uses a non-jquery javascript function to complete the
I have a timer every 20 seconds using setInterval, it uses jQuery's ajax function
I'm using MVC3 - i have a javascript function that uses jQuery get() to
I have a dialog window that uses a URL for its contents { function(){jQuery.ajax({'success':function(html)
I have a JQUERY function that uses AJAX via a PHP page, when the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.