use*_*534 2 javascript settimeout
我需要运行计时器的javascript函数,当计时器到期时,它调用函数calllink()设置新的x1变量女巫值然后传递给settimeout持续时间延迟.
例如......这是代码:
var x1 = 5000;
function calllink() {
alert("Delayed " + x1);
x1 = 1000;
}
setTimeout(function() { calllink(); }, x1);
Run Code Online (Sandbox Code Playgroud)
因此,当运行此代码时,它需要先延迟5秒然后显示消息:延迟5000然后再次运行setTimeout并且当时间到期显示消息延迟1000
我需要这个用于编程epg,所以我读取xml文件,从xml文件读入变量x1以获取通道持续时间(endtime - starttime)然后执行此函数,当时间到期时调用calllink()然后从xml读取下一个程序持续时间(endtime-starttime)然后设置设置为setTimeout x1的变量x1的新值.
如果这可能吗?我今天正在尝试,但没有去.我总是延迟1秒的消息.
更新的问题:
好的...因为我需要在列表中读取130个频道的epg,所以我将此函数放在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] );
}
Run Code Online (Sandbox Code Playgroud)
和json调用,检索有关显示时间,开始时间,结束时间和描述的信息
// 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" );
});
Run Code Online (Sandbox Code Playgroud)
}
我的最大变量值为130 ..所以我试图将定时器变量名增加到130(定时器[0],定时器[1] ....定时器[129]并将值seconds[count]放入该定时器变量(定时器[0] =秒[0],定时器[1] =秒[1] .....定时器[129] =秒[129]).
然后当时间结束然后调用函数epg(count)检索show的新数据信息并将变量设置seconds[count]为新值,刷新li元素与新显示名称durataion ...
所以问题是如何循环130个通道用于130个通道,当一个或多个计时器到期时,它用新值刷新该通道ul?
你需要另外将你的setTimeout调用添加到你的calllink函数本身,使它像这样递归:
var x1 = 5000;
function calllink() {
alert("Delayed " + x1);
x1 = 1000;
setTimeout(function() { calllink(); }, x1);
}
setTimeout(function() { calllink(); }, x1);
Run Code Online (Sandbox Code Playgroud)
请注意,您应该添加某种条件,以便它不会永远重复,除非您希望它执行此操作.