Javascript, jQuery, Do function after another function - Stack Overflow

I have this javascript function.<script type="textjavascript">$(document).ready(functi

I have this javascript function.

<script type="text/javascript">
  $(document).ready(function () {
  $('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>');
  $('#loaded').fadeIn(1500);
     $('#load').fadeOut(2000);
  });
</script>

As you may see it simply runs a loading image for a tot of seconds, then shows the div and hide the loading image. This is not good for me. I need a function that will work in this way. The logic of the code would be:

As soon as $('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>'); is running, then wait 4 seconds, and then show the #loaded DIV.

In this way the #loaded DIV will be fully loaded and it will work fine, since it is an heavy one.

In few words I must to be sure that #loaded has been fully loaded by the browser before showing it. How can I create a function that will do that?

On document ready show the loading DIV. Then WAIT for 3-4 seconds, then make the loading disappear and the loaded DIV visible.

Is this possible?

I have this javascript function.

<script type="text/javascript">
  $(document).ready(function () {
  $('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>');
  $('#loaded').fadeIn(1500);
     $('#load').fadeOut(2000);
  });
</script>

As you may see it simply runs a loading image for a tot of seconds, then shows the div and hide the loading image. This is not good for me. I need a function that will work in this way. The logic of the code would be:

As soon as $('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>'); is running, then wait 4 seconds, and then show the #loaded DIV.

In this way the #loaded DIV will be fully loaded and it will work fine, since it is an heavy one.

In few words I must to be sure that #loaded has been fully loaded by the browser before showing it. How can I create a function that will do that?

On document ready show the loading DIV. Then WAIT for 3-4 seconds, then make the loading disappear and the loaded DIV visible.

Is this possible?

Share Improve this question edited May 16, 2011 at 23:38 OMG Ponies 333k85 gold badges535 silver badges508 bronze badges asked May 16, 2011 at 23:30 DiegoP.DiegoP. 45.8k34 gold badges90 silver badges110 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

make your fadein launch as a callback of your fadeout.

    $(document).ready(function() {
        $('#load')
          .html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>')
          .fadeOut(2000, function() {
                $('#loaded').fadeIn(1500);
           });
    });

But to do this properly i would add the #load div to the html, hide it via css. Then simply do the fadeOut on document.ready.

update.

Mixing the best of both Andrew and my answer, this should work flawlessly:

<div id="load" style="display:none">Loading, please wait</div>

$(document).ready(function() {
        $('#load').show();
    $('#loaded').load(function() {
        $('#load').fadeOut(500,function(){  $(this).fadeIn(500);});
    });
});

Your code won't "load" the image. All it does is add the image tag to the DOM. The browser will try to add the image once the tag is added to the DOM.

Your best bet is to add the image to the HTML code and hide it with css. Then use jQuery's show function to show it rather than try to load the image.

An alternative would be to "preload" the image with javascript, but I don't see how that would be more efficient.

You could bind a handler to the load event on the #loaded div:

$(document).ready(function() {
    $('#loaded').load(function() {
        $('#loaded').fadeIn(500);
        $('#load').fadeOut(500);
    });
});

See http://api.jquery./load-event/

I am a little confused by all these answers and not sure what your question is or if I am right or they are right, but this is how to wait 4 seconds. (added 2 lines to your original code)

<script type="text/javascript">
  $(document).ready(function () {
    $('#load').html('<img style="display:block; margin:10px auto;background-color: #EEE;" src="<?echo $site["url"];?>images/icons/loading.gif"/>');
    setTimeout(function () {
      $('#loaded').fadeIn(1500);
      $('#load').fadeOut(2000);
    }, 4000)
  });
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745221281a4617260.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信