停止 jQuery 重复
我有一个问题。我有一个脚本,首先检查 iframe 的内容是否已加载。
- 加载内容后,会出现倒计时器。
- 一旦计时器达到 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.
- When the content is loaded, a countdown timer will appear.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是jquery ajax 的大多数情况。
但你可以通过将 ajax 异步默认设置为 false 来解决它。
并将其放在文档准备下。
this is most case the jquery ajax.
but you can solve it by set up the ajax async by defaut to false.
and put it under document ready.
使用
clearTimeout
方法来停止不再需要的超时。声明一个变量来保存计时器的句柄,并将
setTimeout
的结果分配给它。如果再次调用该函数并设置句柄,则清除计时器:演示: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:Demo: http://jsfiddle.net/Guffa/deeWy/1/
试试这个
Try this