Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
having a function run every ten seconds or when an input changes

I have a simple react app where a user inputs a quantity and an api call is made to convert that value into an equivalent amount of a very unstable cryptocurrency, and that converted value is displayed on the screen.

Crucially, I want the converted value to be updated every ten seconds or when a user changes the input value.

I'm confused as to how to implement this. Any advice?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I don't know react, but in native JS it's pretty simple. Here's some code that sets your timer then clears and resets when you call an input change. Hope this is easily adaptable to react!

function cryptoTimer() {
  this.value = null
  this.timer = null
  this.getValue = () => {
    this.value = "some api response"
    console.log(this.value)
  }
  this.startTimer = () => {
    this.timer = setInterval(() => { this.getValue() }, 10000)
  }
  this.clearTimer = () => {
    clearInterval(this.timer)
  }
  this.inputChange = (elem) => {
    console.log('input changed, value is '+elem.value)
    this.clearTimer()
    this.init()
  }
  this.init = () => {
    this.getValue()
    this.startTimer()
  }
  this.init()
}

var timer = new cryptoTimer
<!-- some input on change action -->
<input onkeyup="timer.inputChange(this)" type="text" />

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an example

import React from "react";

export function App() {
  // Used to store the intervalId
  const timerRef = React.useRef();

  // Used to store the value in the input element
  const inputRef = React.useRef("");

  // This is for storing the transformed value you get from API
  const [transformedValue, setTransformedValue] = React.useState("");

  // In this function, you can make the request to the API and update
  // the transformed value. Remember to reset the interval.
  const transformValue = async () => {
    const input = inputRef.current;

    // Invoke your api instead
    const randomTransformation = `${input} ${new Date().toLocaleTimeString()}`;

    setTransformedValue(randomTransformation);

    resetInterval();
  };

  // This will just reset the interval
  const resetInterval = () => {
    if (timerRef.current) {
      clearInterval(timerRef.current);
    }
    timerRef.current = setInterval(transformValue, 10 * 1000);
  };

  return (
    <div className="container">
      <label htmlFor="input">Input</label>
      <input
        id="input"
        type="text"
        value={inputRef.current}
        onChange={(e) => {
          inputRef.current = e.target.value;
          transformValue();
        }}
      />

      <p>Transformed Value: {transformedValue}</p>
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda