admin管理员组

文章数量:1277290

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( ".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?

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./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?

Share Improve this question edited Jan 21, 2014 at 11:09 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Jan 20, 2014 at 21:46 user2631534user2631534 4774 gold badges9 silver badges27 bronze badges 2
  • function() { calllink(); } is effectively the same as calllink, at least in this case. – Tobias Commented Jan 20, 2014 at 21:50
  • possible duplicate of javascript while loop or set interval with dynamic delay – JLRishe Commented Dec 19, 2014 at 4:18
Add a ment  | 

2 Answers 2

Reset to default 6

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.

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.

本文标签: javascript settimeout change delay in function callStack Overflow