Javascript 在一个setInterval下的不同时间执行多个函数(函数()
我现在有一个倒数计时器,每30秒刷新一个图像。这段代码对我来说一直很好Javascript 在一个setInterval下的不同时间执行多个函数(函数(),javascript,jquery,Javascript,Jquery,我现在有一个倒数计时器,每30秒刷新一个图像。这段代码对我来说一直很好 <script type="text/javascript"> jQuery(document).ready(function() { var countdown = 30; jQuery('#countdown').html(countdown); setInterval(function() { countdown--; jQuery('#countdown').html(countdown)
<script type="text/javascript">
jQuery(document).ready(function() {
var countdown = 30;
jQuery('#countdown').html(countdown);
setInterval(function() {
countdown--;
jQuery('#countdown').html(countdown);
if (countdown <= 0)
{
jQuery('.imagerefresh').attr('src', jQuery('.imagerefresh').attr('src'));
countdown = 30;
jQuery('#countdown').html(countdown);
}
}, 1000);
});
</script>
jQuery(文档).ready(函数(){
var倒计时=30;
jQuery('#倒计时').html(倒计时);
setInterval(函数(){
倒计时--;
jQuery('#倒计时').html(倒计时);
如果(倒计时)那么停止使用setInterval怎么样
$(函数(){
var max=30;
var倒计时=最大值;
var img=$(“#图像”);
变量显示=$(“#倒计时”);
模拟加载(功能(事件){
设置超时(刷新,1000);
});
var load=函数(url){
var ts=(新日期()).getTime();
img.attr('src',url+'?ts='+ts);
};
var get=function(){
//$.get('/path/to/file/getnewimage.php',load);
setTimeout(函数(){
负载('http://lorempixel.com/400/200/');
}, 500);
};
var refresh=function(){
text('等待'+倒计时+'秒…');
倒计时--;
如果(倒计时<0){
倒计时=最大值;
get();
}否则{
设置超时(刷新,1000);
}
};
get();
});
并注意图像加载。我甚至不会等到你倒计时的第20秒才获得图像。当你刷新当前图像时,我会立即将其预加载,并将其保存在内存中,直到下次倒计时结束时需要它。关于崩溃…你可以向visua提供的任何实时演示理睬问题?我不知道你所说的“自我崩溃”是什么意思,但是关于您将?…
查询字符串添加到URL末尾的方法,在整个循环的第二次和后续时间,您将在URL末尾添加额外的问号和时间戳,即URL将越来越长。@BZCS,您觉得我的答案有用吗?等等,让我更新它!仍然有问题吗更新问题-似乎在倒计时“0”时被卡住,尝试在倒计时“0”时获取新图像,但从未刷新页面上的图像,不想循环。感谢视频链接,非常有用-但无法使上述代码在我的情况下工作。我仍然有刷新时未显示图像的问题(“崩溃”)正如我在原始代码中提到的那样)。我还需要了解如何将图像获取代码与图像显示代码进行协调,因为它当前似乎在图像完全下载之前显示(我在初始页面加载中看到了这一点)。您需要取消对$的注释。获取并删除虚拟timeoutSorry,一直在测试并尝试使其工作。似乎在倒计时“0”时卡住,尝试在倒计时“0”时获取新图像,但从未刷新页面上的图像,并且不希望循环。
if (countdown <= 10)
{
jQuery.get('/path/to/file/getnewimage.php');
}
$(function() {
var max = 30;
var countdown = max;
var img = $('#image');
var display = $('#countdown');
img.load(function(event) {
setTimeout(refresh, 1000);
});
var load = function(url) {
var ts = (new Date()).getTime();
img.attr('src', url + '?ts=' + ts);
};
var get = function() {
//$.get('/path/to/file/getnewimage.php', load);
setTimeout(function() {
load('http://lorempixel.com/400/200/');
}, 500);
};
var refresh = function() {
display.text('wait ' + countdown + ' seconds...');
countdown--;
if(countdown < 0) {
countdown = max;
get();
} else {
setTimeout(refresh, 1000);
}
};
get();
});