哈尔执行

时间:2016-08-12 10:41:40

标签: javascript php

我的页面上有一个计时器,因为计时器到期时它正在提示警告框,现在问题是甚至在警告框中按OK按钮后再次调用。实际上,我想将其重定向到另一个页面。 window.location="";可以正常工作,但它会进入减去计时器。

HTML CODE:

<div>Registration closes in <span id="time">00:03</span> minutes</div>

JAVASCRIPT CODE:

<script   src="https://code.jquery.com/jquery-3.1.0.js"   integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk="   crossorigin="anonymous"></script>  

    <script type="text/javascript">
        function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    setInterval(function () {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.text(minutes + ":" + seconds);

        if (--timer < 0) {
            alert("I am an alert box!");
            //window.location = "http://www.google.com";
        }
    }, 1000);
}

jQuery(function ($) {
    var fiveMinutes = 0.5 * 5,
        display = $('#time');
    startTimer(fiveMinutes, display);
});
    </script>

3 个答案:

答案 0 :(得分:1)

完成使用后的时间间隔:clearInterval

window.top !== window.self

答案 1 :(得分:1)

您必须保存间隔的参考,以便在需要时将其停止。看看这个:

&#13;
&#13;
var interval;
function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    interval = setInterval(function () {
        minutes = parseInt(timer / 60, 10)
        seconds = parseInt(timer % 60, 10);

        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.text(minutes + ":" + seconds);

        if (--timer < 0) {
            alert("I am an alert box!");
            clearInterval(interval);
        }
    }, 1000);
}

$(function ($) {
    var fiveMinutes = 0.5 * 5,
        display = $('#time');
    startTimer(fiveMinutes, display);
});
 
&#13;
<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></script>  
<div id="time" />
&#13;
&#13;
&#13;

但它更愿意使用此代码

setTimeout(function(){ startTimer(fiveMinutes, display); }, 1000);

然后进入startTimer函数,根据你的逻辑,再次调用setTimeout。

希望它有所帮助!

答案 2 :(得分:0)

clearInterval()将返回一个标识符,您应该将该标识符分配给变量。

要停止调用间隔函数,您需要<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></script> <script type="text/javascript"> function startTimer(duration, display) { var timer = duration, minutes, seconds; myInterval = setInterval(function () { minutes = parseInt(timer / 60, 10) seconds = parseInt(timer % 60, 10); minutes = minutes < 10 ? "0" + minutes : minutes; seconds = seconds < 10 ? "0" + seconds : seconds; display.text(minutes + ":" + seconds); if (--timer < 0) { alert("I am an alert box!"); clearInterval(myInterval); //window.location = "http://www.google.com"; } }, 1000); } jQuery(function ($) { var fiveMinutes = 0.5 * 5, display = $('#time'); startTimer(fiveMinutes, display); }); </script>

您的代码应如下所示:

toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //getSupportActionBar().setLogo(R.drawable.logo_nuevo);

       /* getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setIcon(R.drawable.logo_nuevo); */

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);

        mDrawer.addDrawerListener(drawerToggle);
        nvDrawer = (NavigationView) findViewById(R.id.nvView);
        nvDrawer.inflateHeaderView(R.layout.nav_header);
        setupDrawerContent(nvDrawer);