停止 jQuery 重复

发布于 2024-11-28 14:14:30 字数 2035 浏览 0 评论 0原文

我有一个问题。我有一个脚本,首先检查 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>

I have an issue. I have a script, that first checks if the content of an iframe is loaded.

  1. When the content is loaded, a countdown timer will appear.
  2. Once the timer hits 0, a form will run.

My issue is that when the iframe is loaded, and when the countdown begins, I can click on another link in the iframe, and it will start the timer once again.

My script is:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长安忆 2024-12-05 14:14:30

这是jquery ajax 的大多数情况。

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

$.ajaxSetup({async:false});

并将其放在文档准备下。

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

this is most case the jquery ajax.

but you can solve it by set up the ajax async by defaut to false.

$.ajaxSetup({async:false});

and put it under document ready.

$(document).ready(function() {
      $.ajaxSetup({async:false});
      // another code here...
});
空宴 2024-12-05 14:14:30

使用 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/

Use the clearTimeout method to stop a timeout that is no longer wanted.

Declare a variable to hold the handle to the timer, and assign the result of setTimeout to it. Clear the timer if the function is called again and the handle is set:

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();
      }
   }
}

Demo: http://jsfiddle.net/Guffa/deeWy/1/

吃颗糖壮壮胆 2024-12-05 14:14:30

试试这个

    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();
            }
        }

Try this

    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();
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文