#1
  1. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4

    Question How to interrupt a queue?


    I have a situation where a web application has to display a message
    for 30 seconds. I could use something like this:

    Code:
    $('#lognote').text(startMessage).delay(30000).text("");
    But the application may have to display another message before the
    30 seconds has elapsed. How can I interrupt the queue to cause the
    new message to display before the 30 seconds has elapsed?
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,165
    Rep Power
    184
    You could do something like this, because the delay() does not seem to work, as you desire it to; when tested:

    Code:
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    
    <div id="lognote"></div>
    
    <script>
    var startMessage="Hello";
    var interuptMessage="Goodbye =(";
    $('#lognote').text(startMessage);
    setTimeout(function() {
    $('#lognote').text(interuptMessage);
    },"5000");
    setTimeout(function() {
    $('#lognote').text(startMessage+" Again!");
    },"15000");
    </script>

    Comments on this post

    • richpri agrees : Thanks a lot!
  4. #3
  5. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4

    Lightbulb


    Thank you web_loone08 for showing me the setTimeout() function. I was able to use it [along with the clearTimeout() function] to create a function that does exactly what I wanted. This is the tested version of this function.

    javascript Code:
    var BD18 = {};
    BD18.noteTimeout = null;
     
    /* Function doLogNote displays a lognote for 30 seconds.
     * A new lognote will replace any previous log note 
     * that has not yet timed out. BD18.noteTimeout is a
     * global variable with an initial value of null.
     */
    function doLogNote(note) {
      if(BD18.noteTimeout != null) {
        clearTimeout(BD18.noteTimeout);
      }
      $('#lognote').text(note);
      BD18.noteTimeout = setTimeout(function() {
        $('#lognote').text("");
        BD18.noteTimeout = null;
      },"20000");
    }

IMN logo majestic logo threadwatch logo seochat tools logo