取消鼠标单击事件时的javascript重定向

取消鼠标单击事件时的javascript重定向,javascript,redirect,Javascript,Redirect,我想取消由javascript函数启动的重定向。 javascript会在5秒内重定向到主页,并显示为错误消息。我想在鼠标单击错误消息时取消重定向。请帮忙 PHP代码: <div id="error"> <?php //redirect if (isset($_GET['err'])){ if ($_GET['err'] == 1){ echo "You are successfully logged out.

我想取消由javascript函数启动的重定向。 javascript会在5秒内重定向到主页,并显示为错误消息。我想在鼠标单击错误消息时取消重定向。请帮忙

PHP代码:

<div id="error">    
<?php
    //redirect
    if (isset($_GET['err'])){
        if ($_GET['err'] == 1){
            echo "You are successfully logged out. You will be redirected to the home page in <strong><span id = 'seconds'> 5 </span> </strong> seconds.";
        }elseif($_GET['err'] == 0){
            echo "Your session has been expired. Please login again.!";
        }
    }
?>  
</div>


实际上,您正在尝试停止setInterval,您可以通过先命名它,然后在需要的条件下清除它来轻松地完成此操作

var redirInt = setInterval(function() {
    // etc.
},5000);
还有你的取消电话

clearInterval(redirInt);

实际上,您正在尝试停止setInterval,您可以通过先命名它,然后在需要的条件下清除它来轻松地完成此操作

var redirInt = setInterval(function() {
    // etc.
},5000);
还有你的取消电话

clearInterval(redirInt);
试试这个:

<script>

    var seconds = 5;
    var err = <?php echo $_GET['err'] ; ?>;
    var redir = null;

    if (err == 1){

        var redir_interval = setInterval(
        function(){
            if (seconds <= 1) {
                redir = window.location = 'http://****.***.***.***/***';
            }
            else {
                document.getElementById('seconds').innerHTML = --seconds;
            }
            },
            1000
        );
    }

    function cancel_redir() {

        redir = null;
        clearInterval(redir_interval);

    }

</script>

<div id="error" onclick="cancel_redir();">
You are successfully logged out. You will be redirected to the home page in <strong><span id = 'seconds'> 5 </span> </strong> seconds.
</div>

var秒=5;
var err=;
var-redir=null;
如果(错误==1){
var redir_间隔=设置间隔(
函数(){
如果(秒尝试以下操作:

<script>

    var seconds = 5;
    var err = <?php echo $_GET['err'] ; ?>;
    var redir = null;

    if (err == 1){

        var redir_interval = setInterval(
        function(){
            if (seconds <= 1) {
                redir = window.location = 'http://****.***.***.***/***';
            }
            else {
                document.getElementById('seconds').innerHTML = --seconds;
            }
            },
            1000
        );
    }

    function cancel_redir() {

        redir = null;
        clearInterval(redir_interval);

    }

</script>

<div id="error" onclick="cancel_redir();">
You are successfully logged out. You will be redirected to the home page in <strong><span id = 'seconds'> 5 </span> </strong> seconds.
</div>

var秒=5;
var err=;
var-redir=null;
如果(错误==1){
var redir_间隔=设置间隔(
函数(){

if(seconds
setInterval
返回它创建的计时器的ID,以便您可以使用
clearInterval
取消它

您可以使用以下内容:

var timer;

$(document).ready(function() {
    timer = setInterval(function() {...}, 1000);

    $('#error').click(function() {
        clearInterval(timer);
    });
});

setInterval
返回它创建的计时器的ID,以便您可以使用
clearInterval
取消它

您可以使用以下内容:

var timer;

$(document).ready(function() {
    timer = setInterval(function() {...}, 1000);

    $('#error').click(function() {
        clearInterval(timer);
    });
});


您在哪里处理单击事件?您是否尝试过处理它?提示:您必须在另一个函数中使用
clearInterval
清除间隔。为此,您需要引用原始间隔
var myRedirectionInterval=setInterval(..)
。您想在错误消息上单击取消,但我看不到您的代码中有任何地方显示错误消息以便用户可以单击。我使用jquery淡出错误。您在哪里处理单击事件?您是否尝试过处理它?提示:您必须在另一个有趣的游戏中使用
clearInterval
清除间隔为此,您需要一个对原始间隔的引用
var myRedirectionInterval=setInterval(..)
。您想在错误消息中单击时取消,但我看不到您的代码中显示错误消息以便用户可以单击它的任何地方。我已使用jquery淡出错误。没有解释,不适用于当前情况:(.Edit:和突然出现的jquery.Yes-我看到了jquery注释:)Woops,忘记附加的jQuery注释,我浏览了OP的注释^^“。没有解释,不适合当前情况:(.编辑:突然间,jQuery。是的,我看到了jQuery注释:)Woops,忘记附加的jQuery注释,我浏览了OP的注释^”。他还在他的例子中提供了倒计时,所以在这种情况下,
setInterval
似乎是合理的。@Zeta:我似乎已经略过了那部分。谢谢!他在他的例子中也提供了倒计时,所以在这种情况下,
setInterval
似乎是合理的。@Zeta:我似乎略过了那部分。谢谢!谢谢inhan。它的工作原理就像一个charm。也非常感谢所有试图帮助我的人。没问题。如果您使用
setTimeout()
函数(setInterval的一次性版本),则需要使用
clearTimeout()
函数。为了能够操作任何间隔函数,您需要将其命名为setInterval或setTimeout。感谢inhan。它工作起来很有魅力。也非常感谢所有试图帮助我的人。没问题。如果您使用的是
setTimeout()
函数(setInterval的一次性版本)您将需要使用
clearTimeout()
函数。要能够操作任何interval函数,您需要将其命名为setInterval或setTimeout。