标签: setinterval

为什么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
查看次数

为什么 setTimeout 和 setInterval 在浏览器和 Node.js 中的工作方式不同?

let cnt=0;

let i = setInterval(() => {
  console.log(cnt++);
},200)

setTimeout(() => {
  clearInterval(i);
},2000);
Run Code Online (Sandbox Code Playgroud)

当在浏览器中执行时,此代码会记录:- 0 1 2 3 4 5 6 7 8 9

但是当使用 Node 执行时,它会记录:- 0 1 2 3 4 5 6 7 8

这是什么原因呢?

javascript browser settimeout setinterval node.js

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

如何在 React 中的功能组件上使用 setInterval 更新状态

我正在尝试实现倒计时,但状态没有按预期更新。它停留在初始值 30。我不知道如何解决它。有人可以帮我吗?

  const [timer, setTimer] = useState(30);

  function handleTimer() {
    const interval = setInterval(() => {
      setTimer((count) => count - 1);
      if (timer <= 0) {
        clearInterval(interval);
      }
    }, 1000);
  }

  useEffect(() => {
    handleTimer();
  }, []);
Run Code Online (Sandbox Code Playgroud)

timer setinterval reactjs react-hooks

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

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
查看次数

setInterval和window.onload问题

我有这个代码

window.onload = function() {            
    function foo() {
        alert("test");
    }
    setInterval("foo()",500)
}
Run Code Online (Sandbox Code Playgroud)

哪个返回undefined ...当我在window.onload之外使用它时它工作.谁能解释我为什么?

javascript setinterval

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

为什么我不能将javascript setInterval与外部文件中的函数一起使用?

以下工作完美正常...每10秒显示一次警报

<script type='text/javascript'>
    function letsTest(){
        alert("it works");
    }
    var uptimeId = window.setInterval(letsTest, 10000);
</script>
Run Code Online (Sandbox Code Playgroud)

但是当我将我的letsTest函数放在一个名为javaScript.js的文件中时,它就不再有效了.

主页:

<script src='lib/javaScript.js' type='text/javascript'>
    var uptimeId = window.setInterval(letsTest, 10000);
</script>
Run Code Online (Sandbox Code Playgroud)

javaScript.js

function letsTest(){
    alert("it works");
}
Run Code Online (Sandbox Code Playgroud)

我在路径和拼写上验证了一千次.我javaScript.js也在其他地方使用我.是否可以使用另一个文件中的函数设置间隔?

javascript setinterval

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

如何在Apache tomcat服务器启动时定期自动运行我的java函数?

我有一个java函数,它检查和修改我的SQL数据库中的值以避免错误,我需要它在服务器启动时以及重新启动时自动执行.我创建了一个jsp页面来调用这个函数作为jsp支持"setInterval",我可以在每3分钟后自动运行它以从我的数据库中删除错误,现在我需要它在服务器启动时自动执行.任何人都可以指导我吗?

以下是我的jsp代码:

setInterval(function(){Autolf();},60000);

function Autolf()
{

$.post('autolgfn.jsp',
        {
    abc:1
        },
        function(response,status,xhr)
        {
            alert(response.trim());

        });

}
Run Code Online (Sandbox Code Playgroud)

上面的代码从连接到数据库的java页面调用函数.请帮我在服务器启动时自动运行它,并在每3分钟后继续运行.提前致谢

java jsp tomcat function setinterval

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

JavaScript多个时间间隔和clearInterval

我有一个小程序,当您单击“条目”时,将打开编辑模式,而该条目是为其他人锁定的。每隔10秒就会发送一个ajax请求来更新表中的时间戳。

$(".entry-edit").click(function() {
  // code

  loopLockingVar = setInterval(function() { loopLockingFunction(id) }, 10000);

  // code
});
Run Code Online (Sandbox Code Playgroud)

然后,我有一个取消按钮,将表中的时间戳更新为0并清除间隔。

$(".entry-cancel").click(function() {
  // code   

  clearInterval(loopLockingVar);

  // code
});
Run Code Online (Sandbox Code Playgroud)

当仅编辑一个条目时,所有操作均有效,但是如果同时处理两个或多个条目,然后单击“取消”,则第一个条目的间隔会更远...

我试过了

var loopLockingVar;
$(".entry-edit").click(function() {
  // code

  if( ! loopLockingVar) {
    loopLockingVar = setInterval(function() { loopLockingFunction(id) }, 10000);
  }

  // code
});
Run Code Online (Sandbox Code Playgroud)

但是,如果您取消并再次单击“编辑”,此方法将无法正常工作。

javascript setinterval clearinterval

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

JavaScript setInterval()冻结了我的网站

我在我的Raspberry Pi3中安装了Apache以获得本地网站.我有这个JavaScript index.php,我需要每200ms刷新一次:

function Function()
{
    $.ajax({
        url : 'variable.php',
        data: {action: 'variable'},
        type: 'post',
        success: function(output){
            document.getElementById("demo").innerHTML= output;
            setInterval(Function(),200);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在variable.php,它只是一个命令的输出.

但是,在2分钟内,可用内存从220 000 KiB变为40 000 KiB,因此我的网站在10分钟内开始冻结,我必须关闭我的网络并重新打开它.

怎么了 ?

javascript setinterval

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

setInterval中的JS类方法不起作用

我有一个带有setInterval的类的简单示例,该类每5秒调用一次main()。谈到print()时,它返回TypeError:this.print不是函数。而且我真的很困。为什么如果我在没有setInterval的情况下调用main(),则它可以正常运行,但是在setInterval的情况下,它将失败?有点奇怪。有没有任何解决方法可在没有此问题的情况下定期调用main()?

"use strict";

class test {
  constructor() {
      this.interval = setInterval(this.main, 5000);
  }

  print(){
      console.log('Teeeessssttt');
  }

  main(){
      this.print();
  }
}

const a = new test();
Run Code Online (Sandbox Code Playgroud)

javascript setinterval

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