JSFiddle - React, Tailwind, and code Playground
by ghost7492
HTML
<div class="wrap">
<div class="btn startMonitor-btn" id="startMonitorBtn">StartMonitor</div>
<div class="btn stopMonitor-btn" id="stopMonitorBtn">StopMonitor</div>
<label class="container">
<span>If checked, reload page.</span>
<input type="checkbox" id="monitorTarget">
<span class="checkmark"></span>
</label>
</div>
CSS
.wrap {
display: flex;
justify-content: center;
margin: 0 auto;
}
.btn {
border-radius: 28px;
font-family: Arial;
color: #ffffff;
font-size: 1rem;
text-decoration: none;
display: grid;
align-content: center;
justify-content: center;
width: 122px;
margin-left: 10px;
}
.btn:hover {
text-decoration: none;
}
.startMonitor-btn {
background-image: linear-gradient(to bottom, #3498db, #2980b9);
}
.startMonitor-btn:hover {
background-image: linear-gradient(to bottom, #3cb0fd, #3498db);
}
.stopMonitor-btn {
background-image: linear-gradient(to bottom, #FF6666, #e24f4f);
}
.stopMonitor-btn:hover {
background-image: linear-gradient(to bottom, #fc8383, #FF6666);
}
/* The following css referred to here. */
/* https://www.w3schools.com/howto/howto_css_custom_checkbox.asp */
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin: 10px 0 12px 10px;
cursor: pointer;
font-size: 22px;
user-select: none;
}
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border: 1px solid #8e8e8e;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px...
JavaScript
const dom = {
startMonitorBtn: document.getElementById('startMonitorBtn'),
stopMonitorbtn: document.getElementById('stopMonitorBtn'),
monitorTarget: document.getElementById('monitorTarget'),
}
let intervalID = null
const TIMEOUT_TIME = 3000//It has been set to 3 seconds for testing.
const isChecked = ({ element }) => {
return element.checked === true
}
const reloadIfNeeded = () => {
console.log('set time has passed.')
if (isChecked({ element: dom.monitorTarget }) === true) {
console.log('it was checked,so reload page.')
window.location.reload()
}
}
const startMonitoring = () => {
console.log('Start monitoring.')
intervalID = setInterval(reloadIfNeeded, TIMEOUT_TIME)
}
dom.startMonitorBtn.addEventListener('click', startMonitoring)
startMonitoring()
dom.stopMonitorbtn.addEventListener('click', () => {
console.log('Stop monitoring.')
clearInterval(intervalID);
})