setinterval

this.method Is not function with setInterval

社会主义新天地 提交于 2021-02-19 04:34:05
问题 I have this simple code: var Modules = (function() { 'use strict'; return { TIMER: function (){ var timer = null; return { time: 100, init: function() { this.counter(); this.timer = window.setInterval(this.counter, 1000); }, counter: function() { this.time -= 1; if (this.time <= 0) { window.clearInterval(this.timer); alert('Time expired'); } console.log(this.time); this.viewer(); }, viewer: function() { document.getElementById('timer').innerHTML = this.time; } } } }; }()); Modules.TIMER()

Using multiple instances of setInterval

女生的网名这么多〃 提交于 2021-02-19 02:33:27
问题 I have a jsFiddle here: http://jsfiddle.net/dztGA/22/ The goal: Essentially, I'm trying to have 2 discrete timers on the same page that can be destroyed and re-created on mouseover/mouseout (pause), or on manual progression (restart). The problem: What my jsFiddle's single timer will illustrate is that when I click "Stop Timer", my setInterval (stored in variable t) seems to have multiple instances albeit being destroyed with clearInterval(t). This becomes apparent when I click "Restart Timer

setInterval() timer not working

荒凉一梦 提交于 2021-02-11 17:47:45
问题 This simple function is meant to display time and update every second using setInterval() , the time is displaying but not updating, I have also tried calling the function from the body's onload call and it is the same. <script language="JavaScript" type="text/javascript"> var currentTime = new Date(); function currentTimeStrings() { var hours = currentTime.getHours(); var hoursToString = hours; if (hoursToString>12) { hoursToString = hoursToString-1; } switch (hoursToString) { case 13:

React Hooks, setTimeout in useEffect not triggering until end, because of state updates

自古美人都是妖i 提交于 2021-02-11 14:21:55
问题 Context : New messages are added (e.g. every two seconds, using setInterval). Messages have status, either old or new. Newly added messages have a 'new' flag. After every 5 seconds, all 'new' messages are designated 'old'. (setTimeout) Problem : Timeout is not triggering until the end. New messages are added, but they remain 'new' until all messages are added. I suspect that after every update the timeout is being reset/cleared and because the updates are occurring faster than the timeout,

How do you run a Javascript function in an interval that starts at the end of callback execution?

荒凉一梦 提交于 2021-02-10 21:01:13
问题 I run a function in Javascript asynchronously by using the setinterval function. myVar = setInterval(myTimer, 5000); The execution of the myTimer function can be quite long, sometimes longer than the specified delay, in that case the intervals just get executed back to back. I would like the next execution of the callback function to be scheduled in relationship with the end of the execution of the previous. To restate I want the myTimer function to run after 5000 ms of when previous finishes

if statement in JavaScript game not working as desired to allow for collision detection

蹲街弑〆低调 提交于 2021-02-10 14:14:50
问题 I'm working through some basic steps in an HTML, CSS and JScript game to get very simple collision detection working between a CHARACTER and an ENEMY. I am looking for something that is simple enough to explain to children of age 8 - 11. If the character and the enemy collide then "game over". I have tried various things, but require some help with the code and an explanation of getComputedStyle method and what Properties it gets, for me to effectively create the main if statement. This is

Run function at set interval only at certain time of the day

 ̄綄美尐妖づ 提交于 2021-02-08 09:55:48
问题 I am currently running a function at regular interval round the clock. setInterval( function(){ do_this(); } , 1000*60); Unfortunately, this is not exactly what I want. I would like this function to be run at set regular interval from morning 0900hrs to 1800hrs only. The function should not run outside of these hours. How can this be done in node.js? Are there convenient modules or functions to use? 回答1: You can simply just check to see if the current time is within the desired time range or

React setInterval in useEffect with setTimeout delay

北城以北 提交于 2021-02-08 07:32:19
问题 I want to run an interval with a delay for the first time it fires. How can I do this with useEffect? Because of the syntax I've found it difficult to achieve what I want to do The interval function useEffect(()=>{ const timer = setInterval(() => { //do something here return ()=> clearInterval(timer) }, 1000); },[/*dependency*/]) The delay function useEffect(() => { setTimeout(() => { //I want to run the interval here, but it will only run once //because of no dependencies. If i populate the

React hooks to manage API call with a timer

余生长醉 提交于 2021-02-08 03:56:23
问题 I need to do a fetch API call that returns a URL, do something with the returned URL, then refresh the URL after 60 seconds. This is something I could comfortably achieve without hooks, but I'd like a hooks solution. IMPORTANT : I'm not looking to refactor this to multiple components, or create custom hooks for either the timer or API call. EDIT : The question is - is this the correct way to handle a timer in a hooks environment? Is there a better way? import React, { useState, useEffect }

Read the contents of a text file every 15 seconds

允我心安 提交于 2021-02-07 12:36:12
问题 I'm working on a music site: I have a text file on the server which contains the name of the currently playing song. I would like read the text file every fifteeen seconds, and change the text displayed on my site, without a refresh. Now, using a little jQuery and javascript, I have actually gotten to the point where the file is read and displayed the first time, but it wont refresh . I have attempted all sorts of setInterval functions, but for the life of me I cant get this part to work. Any