javascript settimeout change delay in function call

11,951

Solution 1

You need to additionally add your setTimeout call into your calllink function itself, to make it recursive like this:

var x1 = 5000;

function calllink() {
    alert("Delayed " + x1);
    x1 = 1000;

    setTimeout(function() { calllink(); }, x1);  
}

setTimeout(function() { calllink(); }, x1);

Note that you should probably add some kind of conditional so that it won't repeat forever, unless that is what you want it to do.

Solution 2

I think this is want you want:

var x1 = getNextFromXml();

function calllink() {
    alert("Delayed " + x1);
    x1 = getNextFromXml();
    setTimeout(calllink, x1);  
};

setTimeout(calllink, x1);

Now you only have to implement the function getNextFromXml.

Share:
11,951
user2631534
Author by

user2631534

Updated on June 14, 2022

Comments

  • user2631534
    user2631534 almost 2 years

    I need javascript function that runs timer and when timer expired it calls function calllink() that sets new x1 variable witch value is then passed to settimeout duration delay.

    For example...here is the code:

     var x1 = 5000;
    
    function calllink() {
        alert("Delayed " + x1);
        x1 = 1000;    
    }
    
    setTimeout(function() { calllink(); }, x1);
    

    So when running this code it needs to delay first 5seconds then display message: Delayed 5000 then run again setTimeout and when time expired display message Delayed 1000

    I need this for programming epg, so i read xml file, read into variable x1 from xml file for channel duration (endtime - starttime) and then execute this function, when time expired it calls calllink() that then read from xml next program duration (endtime-starttime) and then sets new value of variable x1 that is set to setTimeout x1.

    If is this possible to do? I was trying today, and no go. I always getting in delay of 1seconds messages.

    UPDATED Question:

    Ok...since i need to read epg for 130 channels in my list i put this function in onLoad:

    // get epg info for all items
    for (var count = 0; count <= max; count++) {
       epg(count);
       setEPGTimer(count);
    }
    
     // set epg
     function setEPGTimer(count) {
       epg(count);
       setTimeout( function() { setEPGTimer(count); }, seconds[count] );
      }
    

    and json call that retrieves info about show time, start time, end time and description

      // get epg
      function epg(count) {
      // read epg from url
       $.getJSON( "http://mywebsite.com/epg.php?channel=" + epgs[count] + "&type=channel", function( data ) {
        var item = [];
        $.each( data, function( key, val ) {
            if ( typeof (val.epg) != "undefined" && val.epg !== null) {
                item.push( "<li class='epg'><b>" + count + ". " + channel[count] + "</b></br>" + val.epg.start1 + " - " + val.epg.title1 + "</li>" );
    
                // make global variable epg description
                desc[count] = val.epg.desc1;
    
                // convert start and end time to seconds
                var a1 = val.epg.start1.split(':');
                var a2 = val.epg.stop1.split(':');
                var seconds2 = (+a2[0]) * 60 * 60 + (+a2[1]) * 60;
                var seconds1 = (+a1[0]) * 60 * 60 + (+a1[1]) * 60;
                var seconds0 = (seconds2 - seconds1);
    
                // check if is not time in minus
                if (seconds0 > 0) {     
                    seconds[count] = seconds0;
                } else{
                    seconds[count] = 0;
                }
            }
        });
    
        $( ".jTscroller ul#" + count + " li.epg" ).remove();
        $( "<li/>", { "class": "", html: item.join( "" ) }).appendTo( ".jTscroller ul#" + count + ".channels" );
    });
    

    }

    my max variable have value 130..so I was trying to increase timer variable name to 130 (timer[0], timer[1]....timer[129] and put value of seconds[count] into that timer variable (timer[0] = seconds[0], timer[1] = seconds[1].....timer[129] = seconds[129]).

    And then when time is out then calls function epg(count) that retrieves new data info for show and sets variable seconds[count] to new value, refreshed li element with new show name durataion...

    So the question is how can I loop 130timers for 130channels and when one or more timers time expire it refresh that channel ul with new values?

  • JLRishe
    JLRishe over 9 years
    There's no reason to use anonymous functions here. Just pass the function name to setTimeout.
  • Brian Glaz
    Brian Glaz over 9 years
    @JLRishe It's just safer as an anonymous function. Suppose you wanted to add arguments to calllink later