如果按下按钮,如何终止 JavaScript 重定向?
我提出了以下代码,它允许用户在将其重定向离开页面之前查看嵌入电影的页面 30 秒。此外,他们还可以单击链接来隐藏带有倒计时的 div。我需要帮助的是,如果单击该链接,则取消重定向(阻止其发生),以便用户可以继续观看完整的电影。预先感谢您的帮助!
Javascript:
<script type="text/javascript">
var settimmer = 0;
$(function(){
window.setInterval(function() {
var timeCounter = $("b[id=show-time]").html();
var updateTime = eval(timeCounter)- eval(1);
$("b[id=show-time]").html(updateTime);
if(updateTime == 0){
window.location = ("redirect.php");
}
}, 1000);
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".slidingDiv").show();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
});
</script>
HTML:
<div id="my-timer" class="slidingDiv">
You have <b id="show-time">30</b> seconds to decide on this movie.
<a href="#" class="show_hide">Yes, I want to watch this one!</a>
</div>
I have come up with the following code, which allows users to view a page with a movie embed for 30 seconds before redirecting them away from the page. Additionally, they can click a link to hide the div with this countdown. What I need help with is canceling the redirect (stopping it from happening) if that link is clicked, so users can continue to watch the full movie. Thanks in advance for your help!
Javascript:
<script type="text/javascript">
var settimmer = 0;
$(function(){
window.setInterval(function() {
var timeCounter = $("b[id=show-time]").html();
var updateTime = eval(timeCounter)- eval(1);
$("b[id=show-time]").html(updateTime);
if(updateTime == 0){
window.location = ("redirect.php");
}
}, 1000);
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$(".slidingDiv").show();
$(".show_hide").show();
$('.show_hide').click(function(){
$(".slidingDiv").slideToggle();
});
});
</script>
HTML:
<div id="my-timer" class="slidingDiv">
You have <b id="show-time">30</b> seconds to decide on this movie.
<a href="#" class="show_hide">Yes, I want to watch this one!</a>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
setInterval
返回一个值,您可以使用该值通过clearInterval
取消间隔计时器。所以:请注意,我已将变量放入您的
ready
函数中,因此它不是全局变量。离题:您不需要或不想在上面使用
eval
(事实上,您实际上永远不想在以下位置使用eval
一切,对于任何事情)。如果您想解析字符串以生成数字,请使用parseInt
(并且没有任何理由评估像1
这样的文字)。所以这一行:变成
(
10
表示字符串是十进制的——例如,以 10 为基数。)setInterval
returns a value you can use to cancel the interval timer viaclearInterval
. So:Note that I've put the variable inside your
ready
function, so it isn't a global.Off-topic: You don't need or want to use
eval
in the above (in fact, you virtually never want to useeval
at all, for anything). If you want to parse a string to make a number, useparseInt
(and there's never any reason to eval a literal like1
). So this line:becomes
(The
10
means the string is in decimal — e.g., base 10.)您需要使用 clearInterval 方法。
You need to use the clearInterval method.
也许您可以使用 setTimeout()这样做而不是 setInterval()。这是一个 示例。
Maybe you can use setTimeout() to do so rather than setInterval().Here is a sample.