破碎的Foursquare就像动画饲料一样

时间:2011-04-11 02:35:57

标签: javascript jquery

HI,

由于某种原因,根据firebug控制台选项卡未正确定义函数'shift()'。我从教程中直接拿到了这个,不知道我在这里做错了什么。

//animate post feed cycle
 var delay = 2000; // you can change it
  var count = 3; // How much items to animate
  var showing = 2; //How much items to show at a time
  var i = 0;
  function move(i) {
    return function() {
      $('.feed'+i).remove().css('display', 'none').prependTo('#recent_listings');
    }
  }
  function shift() {
    var toShow = (i + showing) % count;
    $('.feed'+toShow).slideDown(1000, move(i));
    $('.feed'+i).slideUp(1000, move(i));
    i = (i + 1) % count;
    setTimeout('shift()', delay);
  }    

 setTimeout('shift()', delay);

标记看起来像这样(双括号是django上下文变量)

    <div id="recent_listings">
    <h2>Recent Listings</h2>
    {% for post in posts %}
        <div class="entries feed{{forloop.counter}}">
        <a href="{{post.get_absolute_url}}">
        <div class="photo">
        <img src="{{site}}media/no-image.png"></img>
        </div>
        <div class="listings">
            <div class="title">
            <h3>{{post.title|truncatewords:5}}</h3>
             </div>
       </div>
           </div>
{% endfor %}
</div>

感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

首先,你没有关闭你的标签(不是真正的问题,但可能会在以后引起其他问题)

其次看起来它可能是一个js上下文问题。您可能希望将您的函数声明为公共函数,以便将移位声明更改为下面的

this.shift = function() {
    //code here
}

答案 1 :(得分:0)

扩展@ d1k_is的答案:

您不应该使用字符串调用setTimeout。字符串很可能不会在您需要的上下文中进行评估。这就是d1k_is建议在全局上下文中定义函数的原因。但是,最好使用对名为

的函数的引用
setTimeout(shift, delay);

BTW,因为您“递归”地呼叫setTimeout,您可能希望改为使用setInterval

相关问题