标签: clearinterval

jquery禁用表单元素

场景是我在表单元素上编写了一个自动刷新功能,我想在登录按钮上点击onclick事件来禁用自动刷新功能.

我无法禁用autorefresh,页面仍然刷新.

我的代码是:

$('#form').ready(function () { //Increment the idle time counter every minute.
    var idleInterval = setInterval("timerIncrement()", 15000); // 1 minute

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e){
        idleTime = 0;
    });
});

function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime > 2) { // 20 minutes
        window.location.reload();
    }
}

$('#login').ready(function () {

    alert("working promptly");
    //Zero the idle timer on mouse movement.
    $(this).click(function (e) {
        alert("Hi In click !"); …
Run Code Online (Sandbox Code Playgroud)

javascript jquery setinterval clearinterval

5
推荐指数
1
解决办法
263
查看次数

在.each中设置JavaScript/jQuery clearInterval

所以我为每个帖子创建了一个间隔,问题是我加载了新帖子并删除了旧帖子,所以显然我想停止之前帖子的间隔.但是我似乎无法弄清楚如何做到这一点.有人可以向我解释如何正确地做这件事吗?我完全迷失了.

$(".post").each(function(){
    myInterval = setInterval("postStats('"+$(this).attr('id')+"')", 500);
});

function postStats(pid) {
    //do some stuff
}

$(".button").click(function(){
    clearInterval(myInterval);
});
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery setinterval clearinterval

5
推荐指数
1
解决办法
447
查看次数

如何停止计时器功能的运行?

我对js有点新鲜,一直试图弄清楚如何在点击按钮时停止运行此功能.我尝试使用clearInterval,但我不确定我是否正确使用它.有人可以看一下这段代码并指出我正确的方向吗?

码:

<div><h1 id="target"></h1></div>
<button id="stop">Stop</button>?
Run Code Online (Sandbox Code Playgroud)

脚本:

var arr = [ "one", "two", "three"];

(function timer(counter) {
     var text = arr[counter];

    $('#target').fadeOut(500, function(){ 
        $("#target").empty().append(text).fadeIn(500);
    });

    delete arr[counter];
    arr.push(text);

    setTimeout(function() {
        timer(counter + 1);
    }, 3000);

    $("#stop").click(function () {
       clearInterval(timer);
    });

})(0);

setInterval(timer);
Run Code Online (Sandbox Code Playgroud)

JS小提琴:http://jsfiddle.net/58Jv5/13/

在此先感谢您的帮助.

javascript jquery clearinterval

5
推荐指数
1
解决办法
2万
查看次数

JS clearInterval或window.clearInterval?

Javascript具有setInterval和clearInterval函数,用于处理异步函数调用.

有没有之间的差异clearInterval(handle)window.clearInterval(handle)

我已经看到它被两种方式使用.

javascript setinterval clearinterval

4
推荐指数
1
解决办法
2032
查看次数

带有history = false autocloses的JQuery Mobile弹出窗口

我试图显示弹出,但弹出自动消失,而历史=假弹出保持可见,但在随后关闭弹出窗口浏览器的后退动作被触发

<div data-role="page" id="indexpage">
    <div data-role="popup" data-history="false" id="appPopup">test popup</div>
    <script>
    $("#indexpage").on("pageshow", function () {
        $("#appPopup").popup("open");
    });
    </script>
</div>
Run Code Online (Sandbox Code Playgroud)

检查这里发生了什么:http: //jsfiddle.net/francisdb/ThtfZ/

有关如何解决此问题的任何想法?

jquery setinterval jquery-mobile clearinterval jquery-mobile-popup

4
推荐指数
1
解决办法
8170
查看次数

如何使用setInterval和clearInterval在javascript中停止计时器十秒?

我想知道如何使用setInterval和clearInterval在javascript中停止计时器十秒后?这是我的代码示例,请你告诉我哪里出错了:

<!DOCTYPE html>
<html>
<head>
<script>

var tt=setInterval(function(){startTime()},1000);


function startTime()
{
  var today = new Date();
  var h=today.getHours();
  var m=today.getMinutes();
  var s=today.getSeconds();
  // add a zero in front of numbers<10
  m=checkTime(m);
  s=checkTime(s);
  today=checkTime(today);
  document.getElementById('txt').innerHTML=s;

}

function checkTime(tt)
{
if (tt==10)
  {
    tt="0" + tt;
    console.log(tt);
    clearInterval(tt);
  }
return tt;
}
</script>
</head>

<body onload="startTime()">
<div id="txt"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript timer settimeout setinterval clearinterval

4
推荐指数
2
解决办法
2万
查看次数

jQuery:停止并启动"setInterval"

我的网站上有幻灯片,但是有问题.

在这里,我的JS:

var size_ini = 1;
$(document).ready(function() {
    setInterval('$("#next").click()',10000)
    $("#next").click(function() {
        if(size_ini < 3);
        size_ini++;
        else
        size_ini = 1
        $(".sample").hide();
        $("#id" + size_ini).show();
        $(".comment" + size_ini).show();
        $(".comment_description" + size_ini).show();
    });
    $("#prev").click(function() {
        if(size_ini > 1)
        size_ini--;
        else
        size_ini = 3;
        $(".sample").hide();
        $("#id" + size_ini).show();
        $(".comment" + size_ini).show();
        $(".comment_description" + size_ini).show();
    });
});
Run Code Online (Sandbox Code Playgroud)

如你所见,我的计时器为10秒.用于幻灯片.我有一个上一个和下一个按钮.因此,当我点击其中一个按钮时,计时器应该停止并再次启动.我尝试过"clearInterval",但这不起作用.

任何人都可以告诉它如何工作.

这是FIDDLE.

html javascript jquery setinterval clearinterval

4
推荐指数
1
解决办法
3万
查看次数

ClojureScript 回调中的 clearInterval

基本上,我想要的是在 ClojureScript 中实现这段代码:

var win = window.open('foo.html', 'windowName');   
var timer = setInterval(function() {   
    if(win.closed) {  
        clearInterval(timer);  
        alert('closed');  
    }  
}, 1000);
Run Code Online (Sandbox Code Playgroud)

我试过这个:

(let [popup (.open js/window "foo.html" "windowName")
      interval (.setInterval
                js/window
                (fn []
                  (when (.-closed popup)
                    (do
                      ;; 'interval' is undefined at this point
                      (.clearInterval js/window interval)

                      (.alert js/window 'closed')))
                1000)]
...)
Run Code Online (Sandbox Code Playgroud)

但是 CLJS 编译器给了我一个interval未定义的警告。

有任何想法吗?

clojure setinterval clojurescript clearinterval

4
推荐指数
1
解决办法
1527
查看次数

为什么useRef可以用来存储react中的setInterval引用而不是普通变量?

我试图理解 React 中的 useRef 钩子。

我在 React 中创建了一个简单的时间。其代码如下。

import { useRef, useState, useEffect } from 'react';

function Parent() {
  const [count,setCount]=useState(5);
  const ref=useRef(0);
//let hold=0;
  const countdown=()=>{
      ref.current=setInterval(()=>{
        // console.log('ref.current-->',ref.current);
        setCount((c)=>c-1)
       
      },1000)
  }
  useEffect(()=>{
    if(count<1)
        clearInterval(ref.current)
  },[count])
  
  return(
      <>
      <h3>Timer : {count}</h3>
      <br/>
      <button onClick={countdown}>countdown</button>
    </>
  )
}

export default Parent;
Run Code Online (Sandbox Code Playgroud)

在这里,我使用钩子创建了一个引用,并且正在监视计数状态。当它达到 0 时,我将调用“clearInteval”函数来清除计时器。

这段代码工作正常。

但是,当我尝试使用普通变量而不是由挂钩创建的变量执行相同操作时,间隔不会被清除。

请在下面找到相同的代码。

import { useRef, useState, useEffect } from 'react';

function Parent() {
  const [count,setCount]=useState(5);
  const ref=useRef(0);
let hold=0;
  const countdown=()=>{
    hold=setInterval(()=>{
        // console.log('ref.current-->',ref.current);
        setCount((c)=>c-1) …
Run Code Online (Sandbox Code Playgroud)

setinterval clearinterval reactjs react-hooks use-ref

3
推荐指数
1
解决办法
2572
查看次数

clearInterval 在使用功能组件的 React 应用程序中不起作用

我想在React使用中构建一个计时器应用程序functional component,以下是要求。

该组件将显示一个初始化为 的0数字counter

该组件将在数字Start下方显示一个按钮counter

单击Start按钮后,计数器将开始运行。这意味着该counter数字将开始每 1 秒增加 1。

当计数器运行(递增)时,该Start按钮将变为Pause按钮。

单击Pause按钮后,counter将保留其值(数字)但停止运行(递增)。

该组件还将显示一个Reset按钮。单击Reset按钮后,counter将转到其初始值(0在我们的例子中)并停止运行(递增)。

下面是我已经实现的代码,但clearInterval似乎不起作用,另外我如何实现重置按钮?

代码:

import React, { useState, useEffect } from "react";

export default function Counter() {
  const [counter, setCounter] = useState(0);
  const [flag, setFlag] = useState(false);
  const [isClicked, setClicked] = useState(false);
  var myInterval;

  function incrementCounter() …
Run Code Online (Sandbox Code Playgroud)

javascript setinterval clearinterval reactjs react-functional-component

3
推荐指数
1
解决办法
6446
查看次数