How to make button in html and java script for appear/disappear in a specific time period? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to make button in html and java script for appear/disappear in a specific time period?

Button which is appear from 11.55am to 11.59pm

28th Oct 2017, 10:38 AM
MOHAMMAD NAEEM
MOHAMMAD NAEEM - avatar
2 Answers
+ 5
You need to get the time using new Date() Then extract hours, minutes and seconds. If hours is in the range 12,23 okay If minutes in the range 55,59 when hours=11, okay. Else not okay. Then if okay, then add the element else don't add it. Do the code yourself
28th Oct 2017, 10:47 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 4
No really need to "extract hours, minutes...", just create a new Date instance, wich default is set to actual datetime, and use its method to set it to the wanted time, then set a function to be run at regular interval test ivalue returned by Date.now() to know in what time range we are and so do hide or show your button (rather than adding/removing): function myTimer() { var now = Date().now; var startTime = new Date(); var endTime = new Date(); startTime.setHours(11,55,0); endTime.setHours(23,59,0); var button = document.getElementById('myButtonID'); if ((now<startTime || now>endTime) && button.style.visibility!='hidden') button.style.visibility = 'hidden'; else if (button.style.visibility!='visible') button.style.visibility = 'visible'; } window.setInterval(myTimer,500);
29th Oct 2017, 4:19 AM
visph
visph - avatar