【问题标题】:Endless loop jQuery?无休止的循环jQuery?
【发布时间】:2016-02-09 09:59:19
【问题描述】:

所以我试图让这个褪色的背景动画在网页上永远持续下去。我该怎么做?代码如下:

    $( document ).ready(function() {
    // Changing the background js code
    console.log('File Loaded! DELETE!!!');
    animate();
});
function animate() {
    window.setInterval(function(){    
    window.setTimeout(function(){
       $('.content').css('background-image','url(http://crondon.guko.co.uk/wp-content/uploads/2015/11/confetti.jpg)');$('.content').css('transition','background 1s linear');$('.content').css('-webkit-transition','background 1s linear');$('.content').css('-moz-transition','background 1s linear');$('.content').css('-o-transition','background 1s linear');
    },3000);
        $('.content').css('background-image','url(http://crondon.guko.co.uk/wp-content/uploads/2015/11/bride_groom_baronial_hall1.jpg)');$('.content').css('transition','background 1s linear');$('.content').css('-webkit-transition','background 1s linear');$('.content').css('-moz-transition','background 1s linear');$('.content').css('-o-transition','background 1s linear');
    },3000);
    animate();
    }

【问题讨论】:

标签: javascript jquery


【解决方案1】:

您不能在 setInterval 中使用 setTimeouts。我认为您正在寻找 setInterval 的功能。看看https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setInterval

一个基本示例(每 1000 毫秒或 1 秒重复一次)是:

setInterval(function() {
    // Code to repeat here
}, 1000);

【讨论】:

  • 有了这个代码,它就不能工作,因为一旦它改变了背景,一旦它不再这样做:/
  • @Callum,听起来那需要调试。您只需拨打setInterval() 一次即可设置周期性事件。您的控制台中是否出现任何 JS 错误?你能设置一个 Fiddle 给读者看吗?
猜你喜欢
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-29
  • 1970-01-01
  • 2011-11-29
  • 2014-07-04
  • 1970-01-01
相关资源
最近更新 更多