1

我有一个脚本,它通过点击函数使用 load() 加载内容。

我想将其更改为每隔一分钟自动加载内容,就像幻灯片一样。

我有 3 个视频需要循环播放。

请有人指出我正确的方向,我不知道从哪里开始。我当前的脚本如下。

谢谢

根据 elliot-bonneville 的回答,我已经更新了下面的代码。我仍然需要找到一种方法来循环播放这些视频。

JS

// define var video
var video = $('div.video');
var linkOne = $('.video_1').attr('href');
var linkTwo = $('.video_2').attr('href');
var linkThree = $('.video_3').attr('href');

setInterval(function() {
// load content into video
video.load( linkTwo + ' .content' , function() {
    // remove loading text
    $('.load').remove();
});
}, 10000) // 60000 milliseconds = one minute

setInterval(function() {
// load content into video
video.load( linkThree + ' .content' , function() {
    // remove loading text
    $('.load').remove();
});
}, 10000) // 60000 milliseconds = one minute

setInterval(function() {
// load content into video
video.load( linkOne + ' .content' , function() {
    // remove loading text
    $('.load').remove();
});
}, 10000) // 60000 milliseconds = one minute
4

2 回答 2

2

Use setTimeout

I think something like the below. I haven't tested it yet, so there might be some syntax errors. But hopefully you understand. let me know if anything doesn't make sense.

One reason I am using setTimeout rather setInterval is that this way, another timer doesn't get fired until after the load has completed. In this case, it won't matter too much as you are only doing it every 1 minute, but in some cases, the server latency might be more then the interval time. Also, this way, if for whatever reason, the script/server goes down and a successful reply isn't received then another call won't be made.

// define var video
var video = $('div.video');
var currentVid=1;
var maxVid=3;

setTimeout(cycleVideo, 10000) // 60000 milliseconds = one minute

function cycleVideo() {
    // load content into video
    var link = $('.video_'+currentVid).attr('href');
    video.load( link + ' .content' , function() {
        // remove loading text
        $('.load').remove();
        if(currentVid==maxVid) currentVid=1; else currentVid++;
        setTimeout(cycleVideo, , 10000) // 60000 milliseconds = one minute

    });
}
于 2012-05-16T04:01:06.213 回答
1

使用setInterval,像这样:

setInterval(function() {
    var link = $('#yourAElementId').attr('href');

    // load content into video
    video.load( link + ' .content' , function() {
        // remove loading text
        $('.load').remove();
    });
}, 60000); // 60000 milliseconds = one minute
于 2012-05-16T03:05:48.343 回答