English 中文(简体)
Why does jQuery fadeOut not work inside this setInterval loop?
原标题:

I m trying to load random items into a div every few seconds, with a nice fadeOut/fadeIn transition between each load. Here s the code:

<html>
  <body>
    <div id="item"></div>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
      // Load a random item
      var item = $( #item );
      function load_item() {
        item.fadeOut(5000, function() {
          item.load( http://dynamic.xkcd.com/comic/random/ #middleContent img , null, function() {
            item.fadeIn(5000);
          });
        });
      };

      // Load initial featured item
      load_item();

      // Schedule repeated loading
      setInterval(load_item, 15000);
    </script>
  </body>
</html>

This works fine the first time through, but on subsequent calls to load_item, the fadeOut() seems to stop working. It doesn t actually fade the #item div out, but jumps immediately into the callback function, ignoring the 5000 delay.

What am I doing wrong?

最佳回答
setInterval("load_item()",15000)

I know it looks funny to put it in quotes, but it works.

The other option is to say:

load_item = function(){...etc...};
setInterval(loadItem,1500)
问题回答

暂无回答




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签