Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

continuously update last updated time react js

import React, { useState, useEffect } from "react";

// store your time variable(dt) in state
const [dt, setDt] = useState(new Date().toLocaleString());

// setup a useEffect with a setInterval that rewrites dt to state (using setDt) every 1 second
useEffect(() => {
    let secTimer = setInterval( () => {
      setDt(new Date().toLocaleString())
    },1000)

    return () => clearInterval(secTimer);
}, []);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #continuously #update #updated #time #react #js
ADD COMMENT
Topic
Name
3+8 =