如果按下按钮,如何终止 JavaScript 重定向?

发布于 2024-11-16 09:48:50 字数 1151 浏览 6 评论 0原文

我提出了以下代码,它允许用户在将其重定向离开页面之前查看嵌入电影的页面 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 技术交流群。

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

发布评论

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

评论(3

安静被遗忘 2024-11-23 09:48:50

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 returns a value you can use to cancel the interval timer via clearInterval. So:

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

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 use eval at all, for anything). If you want to parse a string to make a number, use parseInt (and there's never any reason to eval a literal like 1). So this line:

var updateTime = eval(timeCounter)- eval(1);

becomes

var updateTime = parseInt(timeCounter, 10) - 1;

(The 10 means the string is in decimal — e.g., base 10.)

好多鱼好多余 2024-11-23 09:48:50

您需要使用 clearInterval 方法。

You need to use the clearInterval method.

淡莣 2024-11-23 09:48:50

也许您可以使用 setTimeout()这样做而不是 setInterval()。这是一个 示例

Maybe you can use setTimeout() to do so rather than setInterval().Here is a sample.

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