【问题标题】:Stopping jQuery to repeat停止 jQuery 重复
【发布时间】:2011-08-07 15:38:02
【问题描述】:

我有一个问题。我有一个脚本,它首先检查是否加载了 iframe 的内容。

  1. 加载内容后,会出现倒计时。
  2. 一旦计时器达到 0,就会运行一个表单。

我的问题是,当加载 iframe 时,当倒计时开始时,我可以点击 iframe 中的另一个链接,它会再次启动计时器。

我的脚本是:

<script type="text/javascript">
//Loading bar style
$(document).ready(function() {
    $("#progressbar").progressbar({ value: 0 });
});
// on document load:
$(function() {
   // set "waiting" message:
   $("#loadingStatus").html("Waiting for your advertisements to load...");

   // on iframe load:

   $('#iFrame').load(function() {

       $("#loadingStatus").html($("#isDone").html());



   });
});

$(function count() {
   var seconds = <?php echo $exposure[$r['exposure']]; ?>;
   setTimeout(updateCountdown, 1000);

   function updateCountdown() {
      seconds--;

      if (seconds > 0) {
         $("#countdown").text("You must view this advertisement for " + seconds + " seconds.");
         //$('#progressbar').progressbar({ value: Math.round((seconds/10)*100) });
         setTimeout(updateCountdown, 1000);
      } else {
         submitForm();
      }
   }
});                                                                             


function submitForm() {
                $("#countdown").empty().html('<img src="..ify/dream/images/loading.gif" />');
                $.post(
                    'index.php?i=v&p=k&key=DSF79SADFHSA7D9FGSAD097FSAD7F9779ASDFGS9', 
                    $('form').serialize(),
                    function (data) {
                        proccessData(data);
                    }
                ); 

}  

function proccessData (data) {
            $('#statusF').hide().html('');

            if(data=='success'){
                $('form').fadeOut();
                $('#countdown').addClass('noti-success').html('Advertisement validated!').slideDown();
                redirect("?i=l");  
            }
            else {
                $('#countdown').addClass('noti-error').html(data).fadeIn();
            }
        }

  </script>

【问题讨论】:

  • 请将您的代码设为jsfiddle
  • 查找 '.stop(true)' 和 'queue:' 属性
  • 但是在哪里添加这个停止命令呢?

标签: javascript jquery forms load loading


【解决方案1】:

这是大多数情况下的 jquery ajax。

但您可以通过将 ajax 异步设置为默认为 false 来解决它。

$.ajaxSetup({async:false});

并将其放在准备好的文档下。

$(document).ready(function() {
      $.ajaxSetup({async:false});
      // another code here...
});

【讨论】:

    【解决方案2】:

    使用clearTimeout 方法停止不再需要的超时。

    声明一个变量来保存定时器的句柄,并将setTimeout的结果赋值给它。如果再次调用该函数并设置句柄,则清除计时器:

    var timer = null;
    
    function count() {
       var seconds = <?php echo $exposure[$r['exposure']]; ?>;
       if (timer != null) window.clearTimeout(timer);
       timer = setTimeout(updateCountdown, 1000);
    
       function updateCountdown() {
          timer = null;
          seconds--;
    
          if (seconds > 0) {
             $("#countdown").text("You must view this advertisement for " + seconds + " seconds.");
             //$('#progressbar').progressbar({ value: Math.round((seconds/10)*100) });
             timer = setTimeout(updateCountdown, 1000);
          } else {
             submitForm();
          }
       }
    }
    

    演示:http://jsfiddle.net/Guffa/deeWy/1/

    【讨论】:

    • 我试过这个,但没有运气。 :( 显示示例“您必须查看此广告 7 秒”。然后我单击 iFrame 中的一个链接,它跳回到您必须查看此广告 13 秒,然后再次跳回 7 秒。跨度>
    • @Oliver 'Oli' Jensen:我不确定您的代码中发生了什么...该函数仅在页面加载时运行一次,那么 iframe 中的代码也是如此吗?哪些代码在页面中,哪些代码在 iframe 中?
    • @Oliver 'Oli' Jensen:我去掉了上面代码中函数上的ready事件,并添加了jsfiddle demo。
    【解决方案3】:

    试试这个

        var countDownTimer = null;
        var seconds = <?php echo $exposure[$r['exposure']]; ?>;
    
        function setCountDownTimer(){
          if(countDownTimer)
            clearTimeout(countDownTimer);
    
          countDownTimer = setTimeout(updateCountdown, 1000);
        };
    
        function updateCountdown() {
              countDownTimer = null;
              seconds--;
    
              if (seconds > 0) {
                 $("#countdown").text("You must view this advertisement for " + seconds + " seconds.");
                 //$('#progressbar').progressbar({ value: Math.round((seconds/10)*100) });
                 setCountDownTimer();
              } else {
                 submitForm();
              }
         }
    
    // on document load:
    $(function() {
    
       $("#progressbar").progressbar({ value: 0 });
    
       // set "waiting" message:
       $("#loadingStatus").html("Waiting for your advertisements to load...");
    
       // on iframe load:
    
       $('#iFrame').load(function() {
    
           $("#loadingStatus").html($("#isDone").html());
    
           //Attached click event to the link inside iframe to restart the timer
           var iframe = $('#iFrame');
           iframe.contents().find("linkSelector").click(function(){
             window.top.setCountDownTimer();
           });
    
       });
    
       setCountDownTimer();
    });
    
    
    
    function submitForm() {
                    $("#countdown").empty().html('<img src="..ify/dream/images/loading.gif" />');
                    $.post(
                        'index.php?i=v&p=k&key=DSF79SADFHSA7D9FGSAD097FSAD7F9779ASDFGS9', 
                        $('form').serialize(),
                        function (data) {
                            proccessData(data);
                        }
                    ); 
    
    }  
    
    function proccessData (data) {
                $('#statusF').hide().html('');
    
                if(data=='success'){
                    $('form').fadeOut();
                    $('#countdown').addClass('noti-success').html('Advertisement validated!').slideDown();
                    redirect("?i=l");  
                }
                else {
                    $('#countdown').addClass('noti-error').html(data).fadeIn();
                }
            }
    

    【讨论】:

    • 如果我使用它,当 iframe 加载后,什么都不会出现。没有文字,什么都没有。
    • 是的,您必须在 iframe 加载后执行此操作
    • 如果我必须用我的其余代码输入它,它会是什么样子?我不知道把上面的代码放在哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 2018-08-05
    相关资源
    最近更新 更多