如果按下按钮,如何终止javascript重定向?
我已经想出了下面的代码,它允许用户在将他们从页面重定向出去之前,先查看嵌入电影的页面30秒。此外,他们还可以单击一个链接来隐藏具有此倒计时的div。我需要的帮助是取消重定向(阻止它发生),如果点击该链接,用户可以继续观看完整的电影。提前感谢您的帮助 Javascript:如果按下按钮,如何终止javascript重定向?,javascript,jquery,function,redirect,kill,Javascript,Jquery,Function,Redirect,Kill,我已经想出了下面的代码,它允许用户在将他们从页面重定向出去之前,先查看嵌入电影的页面30秒。此外,他们还可以单击一个链接来隐藏具有此倒计时的div。我需要的帮助是取消重定向(阻止它发生),如果点击该链接,用户可以继续观看完整的电影。提前感谢您的帮助 Javascript: <script type="text/javascript"> var settimmer = 0; $(function(){ window.setInterval(functio
<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>
<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>
var settimmer=0;
$(函数(){
setInterval(函数(){
var timeCounter=$(“b[id=show time]”)html();
var updateTime=eval(计时器)-eval(1);
$(“b[id=show time]”)html(updateTime);
if(updateTime==0){
window.location=(“redirect.php”);
}
}, 1000);
});
$(文档).ready(函数(){
$(“.slidingDiv”).show();
$(“.show_hide”).show();
$('.show_hide')。单击(函数(){
$(“.slidingDiv”).slideToggle();
});
});
HTML:
<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>
<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>
你有30秒的时间来决定这部电影。
setInterval
返回一个值,您可以通过clearInterval
取消间隔计时器。因此:
$(function(){
// +--- Remember the value from `setInterval
// |
// v
var timerHandle = 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);
// + Hook up a handler for the link that uses the handle to clear it
// |
// v
$("selector_for_the_link").click(function() {
clearInterval(timerHandle);
timerHandle = 0;
});
});
请注意,我已将该变量放入ready
函数中,因此它不是全局变量
主题外:您不需要或不想在上面使用
eval
(事实上,您根本不想为任何事情使用eval
)。如果您想解析字符串来生成一个数字,请使用parseInt
(而且没有任何理由去求值像1
这样的文字)。所以这一行:
var updateTime = eval(timeCounter)- eval(1);
变成
var updateTime = parseInt(timeCounter, 10) - 1;
(
10
表示字符串是十进制的-例如,以10为基数。)您需要使用该方法。也许您可以使用它而不是setInterval()。这是一个。非常感谢!我执行了你所有的建议,效果非常好。