setinterval

Function inside “setInterval” does not recieve updated variables from hooks

人走茶凉 提交于 2020-02-07 03:58:26
问题 In useEffect-hook I set interval, which is running function "calculateCircle". There I do some logic, including setting state(with useState-Hook). Variables from hooks are updated, I render and see them on page, but this function keeps consolloging old values. I changed my component to the class-based component (without hooks) and everything is working now. But I wonder which is the problem using hooks. const Features = () => { const block1 = React.createRef(); const shadowText = React

js setInterval定时器

落花浮王杯 提交于 2020-02-02 01:18:43
setInterval():定时器 回去重复执行某一功能 参数1:每次执行的具体任务,自定义 参数2:每次执行的间隔时间,单位毫秒 setTimeout():只执行一次的定时器 参数1:执行的具体时间, 参数2:间隔多久(延迟多久) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> .box{ width: 200px; height: 200px; background-color: pink; line-height: 200px; text-align: center; font-size: 30px; } </style> </head> <body> <div class="box"></div> <script> box=document.getElementsByClassName("box")[0]; box.innerHTML = Math.floor(Math.random()

Use setinterval in React

廉价感情. 提交于 2020-01-25 08:53:07
问题 I'm trying to use setInterval in React but stuck on something I don't properly understand. The code is: const Countdown = () => { const [countdownSecond, setCountdownSecond] = React.useState(0); function x() { console.log(countdownSecond); setCountdownSecond(countdownSecond + 1); } return ( <> <button onClick={() => setInterval(x, 1000)}>Start</button> {countdownSecond} </> ); } The issue is that console always logs to 0. I'm not sure why is that. What concept am I misunderstanding? 回答1: You

setInterval Restart when page is Refreshed

痴心易碎 提交于 2020-01-25 07:57:07
问题 I have the following code to set a counter of tickets sold by increasing the number with a random amount: let tickets = 35000; const contador = document.querySelector('.contador'); let interval = setInterval(function(){ console.log(tickets); if (tickets >= 60000) { var textoTodoVendido = `<p>¡Todo vendido!</p>`; contador.innerHTML = textoTodoVendido; console.log("Sold out"); clearInterval(interval); }else{ var texto = `¡${tickets} entradas vendidas!`; contador.innerHTML = texto; console.log

定时器 setTimeout & setInterval

≡放荡痞女 提交于 2020-01-25 05:18:27
setTimeout和setInteval是window对象上两个主要的定时方法,他们的语法基本相同,但完成功能的却是不同的。 settimeout方法是定时程序,也就是在到达某个指定时间后,执行什么事。(执行一次就拉倒) setinterval方法则是表示间隔一定时间反复执行某些事。 定时器的返回值 当我们设置定时器时(不管是setTimeout还是setInterval),都会有一个返回值。这个返回值是一个数字,代表当前是在浏览器中设置的第几个定时器(返回的是定时器序号)。 let timer1 = setTimeout ( ( ) => { } , 1000 ) console . log ( timer1 ) // 1 let timer2 = setInterval ( ( ) => { } , 1000 ) console . log ( timer2 ) // 2 根据上面两端代码可以知道 1.setTimeout和setInterval虽然是处理不同功能的定时器,但都是浏览器的定时器,所以返回的序号是依次排列的。 2.setInterval设置完成定时器会有一个返回值,不管执行多少次,这个代表序号的返回值不变(设置定时器就有返回值,执行多少次是定时器的处理)。 定时器的清除 clearTimeout([定时器的排队序号]) clearInterval(

html dom SetInterVal()

≡放荡痞女 提交于 2020-01-18 04:20:30
HTML DOM setInterval() 方法 HTML DOM Window 对象 定义和用法 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。 setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或窗口被关闭。由 setInterval() 返回的 ID 值可用作 clearInterval() 方法的参数。 语法 setInterval(code,millisec[,"lang"]) 参数 描述 code 必需。要调用的函数或要执行的代码串。 millisec 必须。周期性执行或调用 code 之间的时间间隔,以毫秒计。 返回值 一个可以传递给 Window.clearInterval() 从而取消对 code 的周期性执行的值。 实例 <html> <body> <input type="text" id="clock" size="35" /> <script language=javascript> var int=self.setInterval("clock()",50) function clock() { var t=new Date() document.getElementById("clock").value=t } </script> </form> <button

ts+react, 用mocha测试setInterval,

为君一笑 提交于 2020-01-17 13:54:56
Question 如何用mocha测试setInterval, 在代码中,将setInterval和clearInterval放在一个函数中,并且用一个公共属性public或private属性来赋值,测试的时候会报错 原因 mocha测试主要是node环境,所以找不到windows对象下的时间 解决方案 先修改开发代码 用组件state来赋值,将setInterval单独放入一个函数,clearInterval单独放入一个函数 /** * #### Starts the timer and update the component's state * @param autoRefreshTimeS is equal component's state interval (s) * ***** * ### Attention: * this is asynchronous operations */ private startTimer(autoRefreshTimeS: number) { this.state.logger.debug("Starting timer with interval", autoRefreshTimeS); let second = 0; const timer = setInterval(async () => { this.state.logger

`setInterval` Apparent Infinite Loop

情到浓时终转凉″ 提交于 2020-01-16 19:41:30
问题 When I open the HTML with the below JavaScript, starting timer gets logged in the console, but then my mouse icon just spins. window.onload = function() { console.log("starting timer"); var n = 0; var id = setInterval(function() { console.log("hello"); n++ }, 100); while(true) { if( n > 5 ) { clearInterval(id); break; } } console.log("finished timer"); } Am I creating some type of infinite loop here? 回答1: JavaScript/DOM is single threaded by design. And while JS is spinning this loop of yours

Python threading - How to repeatedly execute a function in a separate thread?

拈花ヽ惹草 提交于 2020-01-16 14:08:31
问题 I have this code: import threading def printit(): print ("Hello, World!") threading.Timer(1.0, printit).start() threading.Timer(1.0, printit).start() I am trying to have "Hello, World!" printed every second, however when I run the code nothing happens, the process is just kept alive. I have read posts where exactly this code worked for people. I am very confused by how hard it is to set a proper interval in python, since I'm used to JavaScript. I feel like I'm missing something. Help is

js中的setInterval函数

夙愿已清 提交于 2020-01-15 23:04:50
js中,setInterval函数:setInterval(code,milliseconds,[可选参数]) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Interval</title> <style type="text/css"> html,body{ width: 100%; height: 100%;padding: 0; margin: 0; } </style> </head> <body> <script type="text/javascript"> var i=1; function myFun (str){ if(i%2===0){ console.log(str[1]); }else{ console.log(str[0]); } if(++i>100){ cleartInterval(timer); } } var timer=setInterval(myFun,1000,["单数","双数"]); </script> </body> </html> 来源: https://www.cnblogs.com/mrGan/p/6530662.html