“SetInterval Bereaksi” Kode Jawaban

Tetapkan interval bereaksi

useEffect(() => {
  const interval = setInterval(() => {
    console.log('This will run every second!');
  }, 1000);
  return () => clearInterval(interval);
}, []);
Lonely Lyrebird

Fungsi bereaksi dipanggil setiap menit

componentDidMount() {
  this.interval = setInterval(() => this.setState({ time: Date.now() }), 1000);
}
componentWillUnmount() {
  clearInterval(this.interval);
}
Grotesque Gannet

JS SetInterval

function func(){
  console.log("Ran")
}
setInterval(func,1000)//Runs the "func" function every second
DatMADCoder

SetInterval Bereaksi

let myCounter = 0;
let timeout = null;
export default CounterApp = props => {

  const [counter, setCounter] = useState(0);

  // Also don't forget this
  useEffect(()=> {
    return ()=> clearInterval(timeout);
  }, []);      

  myCounter = counter;
  const startInterval = () => {
    timeout = setInterval(() => {
      setCounter(counter => counter + 1);
      console.log("counter: ", myCounter); // counter always return 0 but myCounter the updated value
      if(myCounter === 10) clearInterval(timeout);

    }, 1000);
  };

}
Embarrassed Elk

Jawaban yang mirip dengan “SetInterval Bereaksi”

Pertanyaan yang mirip dengan “SetInterval Bereaksi”

Lebih banyak jawaban terkait untuk “SetInterval Bereaksi” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya