douzhang1115 2016-11-28 04:46
浏览 38
已采纳

如何在jQuery中显示内容之前显示setTimeout图像?

I have button that has a on.click(function() {}. Inside the click function is a setTimeout( function() {} that will show the content from external html file.

<button type="button" class="btn btn-primary choose-iphone-btn">Jet Black</button>

    $(function() {

        $('.choose-iphone-btn').click(function() {

            setTimeout( function() {

                $.get('iphone-checkout.html')
                 .success(function(data) {
                     $('.iphone-pre-order-sales-content-info').html(data);
                });

            }, 3000);

        });

});

I want to display

<img src="images/default.gif" /> 

for 3 seconds before showing iphone-checkout.html.

Any help is appreciated.

  • 写回答

2条回答 默认 最新

  • douju1953 2016-11-28 04:52
    关注

    Set a class to your img, say loading

    <img class="loading" src="images/default.gif" /> 
    

    Show and hide the same within click event.

    $('.choose-iphone-btn').click(function() {
         $(".loading").show(); //get reference to element with its class and show it
         setTimeout( function() {
             $.get('iphone-checkout.html')
               .success(function(data) {
                  $(".loading").hide();//hide once your ajax is success
                  $('.iphone-pre-order-sales-content-info').html(data);
            });
         }, 3000);
    });
    

    You can also hide the loading in .done event, so that even in ajax failure, it doesn't block your view or keep displaying it.

    $('.choose-iphone-btn').click(function() {
         $(".loading").show(); //get reference to element with its class and show it
         setTimeout( function() {
             $.get('iphone-checkout.html')
               .success(function(data) {
                  $('.iphone-pre-order-sales-content-info').html(data);
               });
               .done(function() {
                  $(".loading").hide();//hide once your ajax request is done
               });
          }, 3000);
     });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口