started adding timer
This commit is contained in:
91
util/timer.js
Normal file
91
util/timer.js
Normal file
@@ -0,0 +1,91 @@
|
||||
import { useEffect } from "react";
|
||||
import { useState } from "react";
|
||||
import React from "react";
|
||||
import { View, StyleSheet, Text } from "react-native";
|
||||
import { Button, TextInput } from "react-native-paper";
|
||||
|
||||
export default function Timer() {
|
||||
const [time, setTime] = useState(300000);
|
||||
const [timerOn, setTimerOn] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let interval = null;
|
||||
|
||||
if (timerOn) {
|
||||
interval = setInterval(() => {
|
||||
setTime((prevTime) => prevTime - 1000);
|
||||
}, 10);
|
||||
if (time == 0) {
|
||||
//triger logout();
|
||||
}
|
||||
} else {
|
||||
clearInterval(interval);
|
||||
}
|
||||
|
||||
return () => clearInterval(interval);
|
||||
// if (counter > 0) {
|
||||
// setTimeout(() => setCounter(counter - 1), 1000);
|
||||
// } else {
|
||||
// //triger logout();
|
||||
// }
|
||||
}, [timerOn]);
|
||||
|
||||
return (
|
||||
<>
|
||||
<View>
|
||||
<Text>{time}</Text>
|
||||
</View>
|
||||
<Button mode="outlined" onPress={start}>
|
||||
<Text>{start}</Text>
|
||||
</Button>
|
||||
<Button mode="outlined" onPress={reset}>
|
||||
<Text>{reset}</Text>
|
||||
</Button>
|
||||
<Button mode="outlined" onPress={stop}>
|
||||
<Text>{stop}</Text>
|
||||
</Button>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
function start() {
|
||||
setTimerOn(true);
|
||||
}
|
||||
function stop() {
|
||||
setTimerOn(false);
|
||||
}
|
||||
function reset() {
|
||||
setTime(300000);
|
||||
}
|
||||
|
||||
|
||||
// The data/time we want to countdown to
|
||||
// var countDownDate = new Date(moment.).getTime();
|
||||
// // Run myfunc every second
|
||||
// var myfunc = setInterval(function() {
|
||||
|
||||
// var now = new Date().getTime();
|
||||
// var timeleft = countDownDate - now;
|
||||
|
||||
// // Calculating the days, hours, minutes and seconds left
|
||||
// var days = Math.floor(timeleft / (1000 * 60 * 60 * 24));
|
||||
// var hours = Math.floor((timeleft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
|
||||
// var minutes = Math.floor((timeleft % (1000 * 60 * 60)) / (1000 * 60));
|
||||
// var seconds = Math.floor((timeleft % (1000 * 60)) / 1000);
|
||||
|
||||
// // Result is output to the specific element
|
||||
// document.getElementById("days").innerHTML = days + "d "
|
||||
// document.getElementById("hours").innerHTML = hours + "h "
|
||||
// document.getElementById("mins").innerHTML = minutes + "m "
|
||||
// document.getElementById("secs").innerHTML = seconds + "s "
|
||||
|
||||
// // Display the message when countdown is over
|
||||
// if (timeleft < 0) {
|
||||
// clearInterval(myfunc);
|
||||
// document.getElementById("days").innerHTML = ""
|
||||
// document.getElementById("hours").innerHTML = ""
|
||||
// document.getElementById("mins").innerHTML = ""
|
||||
// document.getElementById("secs").innerHTML = ""
|
||||
// document.getElementById("end").innerHTML = "TIME UP!!";
|
||||
// }
|
||||
// }, 1000);
|
||||
Reference in New Issue
Block a user