2011-08-07 162 views
1

我有一个问题。我有一个脚本,首先检查是否加载了iframe的内容。停止jQuery重复

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

请一个[的jsfiddle(http://jsfiddle.com)代码 –

+0

的查找 '.stop(真)' 和 '队列:' 属性 – Eugene

+0

但是在哪里添加这个停止命令? –

回答

1

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

+0

我尝试过,但没有运气。 :( 它显示的例子“你必须查看这个广告7秒”然后我点击iFrame中的链接,它跳回到你必须看到这个广告13秒,然后再跳回到7秒。 –

+0

@Oliver'Oli'Jensen:我不确定你的代码中发生了什么......该函数只在页面加载时运行一次,iframe中的代码也是这样?代码在页面中,代码在哪iframe? – Guffa

+0

上面的代码是我拥有的所有js –

1

这是大多数情况下,jQuery的阿贾克斯。

但你可以解决它通过设置ajax异步通过defaut为false。

$.ajaxSetup({async:false}); 

并将其置于文档准备好之下。

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

我试过了。工作 –

0

试试这个

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

如果我使用这个,当iframe加载时,不会再出现任何文本,没有任何内容 –

+0

是的,您必须执行此操作iframe加载 – ShankarSangoli

+0

如何它会看起来如果我必须输入它与我的其余代码?我不确定在哪里放置代码。 –