If statement not running inside a setInterval - javascript

Here is the code I'm working with
var d = new Date(), // New Date object
M = d.getMonth(), // Month
D = d.getDate(), // Day of the month
h = d.getUTCHours(), // Hours in 24 hour time
m = d.getUTCMinutes(); // Minutes
console.log(M+'/'+D+' '+h+':'+m);
var href = location.href;
if(M == 1 && D == 13 && h >= 21 && m >= 17){
// It is time so lets just go there
window.location = href+'live';
}else{
// It isn't already time so lets check every 30 seconds
setInterval(checkTime, 1000)
}
function checkTime() {
if(M == 1 && D == 13 && h >= 21 && m >= 17){
// It is time so lets just go there
window.location = href+'live';
}
console.log('checked time');
}
I'm trying to check the date and time and if it's the correct date and time, forward to a different page, if it's not, then check every few seconds (every 1 second for now but I'll probably bump this up to 15 or 30) and check again and if it is now the correct date and time then forward to the new page.
The first if statement works but it doesn't seem to be running the if statement inside of the set interval function.
Maybe I just don't understand how setInterval works totally but I can't see a problem with me code.

Because you are NOT updating the variables, the values never change.
You need to do the date object check every time. They do not update.
The following
d = new Date(), // New Date object
M = d.getMonth(), // Month
D = d.getDate(), // Day of the month
h = d.getUTCHours(), // Hours in 24 hour time
m = d.getUTCMinutes(); // Minutes
needs to be in your checkTime method.

Related

Script to open URL only on first Friday of the month and between 8am-10am

I'm working in Virtual Tour Software with Javascript and want to make an action, which will open an URL (with photo panorama) ONLY in particular part of time (in that case it's first Friday of month and only between 8am and 10am). Any ideas? Thanks Michal
For now I got somethink like this:
var startDate = new Date('Jun 5, 2020 8:00:00').getTime();
var endDate = new Date('Jun 5, 2020 10:00:00').getTime();
setInterval(function() {
var now = new Date().getTime();
var visible = now > startDate && now < endDate;
var hotspot = this.getPanoramaOverlayByName(this.getMediaByName('Panorama'), 'Hotspot');
if(hotspot && hotspot.get('enabled') != visible)
hotspot.set('enabled', visible);
}.bind(this), 1000);`
The problem is that I need to change "var start" and "var end" every time. I want to make it visible on every First Friday of month.
you can use getday() and getdate() function in js to find the day which is friday and date which is less or equal to 7 whatever the month and year is.
you can add following in your code like this:
var d = new Date();
var startDate = d.getDate(); // this will gives you only date
var isFriday = d.getDay(); // this will gives index of the day
var n = d.getHours(); // add this new line gethours
if((startDate <= 7 && isFriday == 5) && (n >= 8 && n <= 10)) // as indexing 5 for friday
{
setInterval(function() {
var hotspot = this.getPanoramaOverlayByName(this.getMediaByName('Panorama'), 'Hotspot');
if(hotspot && hotspot.get('enabled') != true)
hotspot.set('enabled', true);
}.bind(this), 1000);
}
try this.

Problem when equating date values in javascript

I'm running a bit of code that is in encased in a setInterval timer; among other things, before a certain time, it should display a "simulation starts at xxx time" message.
startdate is the time that the simulation begins, and I have defined the other variables as follows:
var d = new Date();
var da = d.getDate();
var h = d.getHours();
var mo = d.getMonth();
if ((mo != 3) || (mo == 3 && da === (startdate - 9))
|| (mo == 3 && da == (startdate - 8) && h < 21)) {
document.getElementById("infoWindow").innerHTML =
"<p style=\"color:red;\">Placeholder text</p>";
}
So, if I set startdate to (say) 10 and the month is April, I should get a message on the 1st, and the 2nd, before 9pm. But when I run the code above it skips the section in curly brackets. I have set my computer clock to ensure that the code should work eg, setting it to April 1st, and April 2nd, any time from midday to 9pm, but I get nothing.
In firefox dubugger, when I put a break point in and look at the value of "da" in the "if..." statement, it says "false" and not a numerical value. Replacing the "===" with "==" makes no difference either as I thought it was getting confused with the returned date and a true/false return from the date commands. I cannot see what is wrong.
In other parts of the code, I have other checks to make sure that the code performs things on time, such as (da==startdate+5) and they run fine, it is just this one section that is causing problems
It works fine for me, i tried startdate = 34, to make today check, then adjust my computer clock, still OK. Don't you forget any <div id="infoWindow"></div> in you html code?
var d = new Date();
var da = d.getDate();
var h = d.getHours();
var mo = d.getMonth();
var startdate = 34;
if( (mo !=3) || ( mo==3 && da===(startdate-9)) || ( mo==3 && da==(startdate-8) && h<21) ) {
document.getElementById("infoWindow").innerHTML = "<p style=\"color:red;\">Placeholder text</p>";
}
<div id="infoWindow"></div>

Javascript Date/Time Check Error

using the following code in my application to display html pages depending on it being todays date and also which time of the day it is e.g morning, afternoon or evening. Currently it is 2:53pm and the code is only displaying the am html page (which is the first one). I tried to run the console.log command but got nothing in the console which could be because of wikitude.
The first function of getting the date is working correctly it is just not checking the time correctly.
var inputDate = new Date("5/17/2018");
// Get today's date
var todaysDate = new Date();
// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
var hour = new Date().getHours();
console.log("hour is: " + hour);
// between 12 PM and 7 AM respectively
if(hour => 7 && hour < 12) {
//morning (Always running code here no matter what time of day)
}
else if(hour >= 12 && hour <= 18) {
//afternoon
}
else {
//evening or before 7
}
}
else{
//not today (works if date is not today)
}
You have a typo in the if statement: => should be >=
var inputDate = new Date("5/17/2018");
// Get today's date
var todaysDate = new Date();
// call setHours to take the time out of the comparison
if (inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
var hour = new Date().getHours();
console.log("hour is: " + hour);
// between 12 PM and 7 AM respectively
if (hour >= 7 && hour < 12) {
//morning (Always displaying code here)
alert('morning')
}
else if (hour >= 12 && hour <= 18) {
//afternoon
alert('afternoon')
}
else {
//evening or before 7
alert('evening')
}
}
else {
//not today
alert('not today')
}

current time plus one hour condition

I am stuck with one condition where I have to change the path based on time.
I will have two date variables, CheckInStartDate and CheckInEndDate, which will be coming from API.
Current time is system date time.
Path has to change in two conditions.
CheckInStartDate minus 1 hour of current time
CheckInEndDate plus 1 hour of current time.
This is the code I currently have.
$scope.checkIn = function() {
var ONE_HOUR = 60 * 60 * 1000; /* ms */
$scope.checkInStartDate= 01/16/2017 09:06:00 AM;
$scope.checkInEndDate= 01/16/2017 11:06:00 AM;
var checkInStartDate=$scope.checkInStartDate;
var checkInEndDate=$scope.checkInEndDate;
var currentDate = new Date();
var checkinStartDate=new Date(checkInStartDate);
var checkinEndDate = new Date(checkInEndDate);
if ((checkinStartDate.getTime()) > (currentDate.getTime() - ONE_HOUR) ||
(checkinEndDate.getTime()) < (currentDate.getTime() + ONE_HOUR)) {
$location.path('/checkIn')
}
else{
alert("cannot checkin");
}
}
If I got your question correctly, you want to allow check-in in the time period between the following two times:
CheckInStartDate minus 1 hour
CheckInEndDate plus 1 hour
Let's call the first point s and the second point e. The current time is c. In this case your condition should be true when
s < c < e
In JavaScript this is equivalent:
var s = new Date($scope.checkInStartDate).getTime() - ONE_HOUR;
var e = new Date($scope.checkInEndDate).getTime() + ONE_HOUR;
var c = new Date().getTime();
if (s < c && c < e) {
// check-in allowed
} else {
// not allowed
}

show/hide div during business hours, mix with UTC?

UPDATE
I have added a current working fiddle, I feel like it should be correct, but it doesn't seem to work.
https://jsfiddle.net/bill9000/yadk6sja/2/
original question:
what I have is some code to show/hide div (basically show div during certain business hours) - is there an easy way to make this use a calculate from UTC... so that the show/hide time is fixed on a certain timezone. eg. 6pm EST instead of 6pm users time... here's my current code:
var d = new Date();
var dayOfWeek = d.getDay();
var hour = d.getHours();
var mins = d.getMinutes();
var status = 'open';
if (dayOfWeek !== 6 && dayOfWeek !== 0 && hour >= 03 && hour < 15){
//if (hour=='10' && mins < '30'){
// status = 'closed';
// }else{
status = 'open';
// }
}else{
status = 'closed';
if (status=='open') {
$b('.orderby').show();
}else{
$b('.orderby').hide();
}
also, I have some other JavaScript that's getting the UTC diff:
function ShowTime() {
var now = new Date();
var diff = now.getTimezoneOffset() / 60; //the difference between local PC and UTC
diff = diff - 4; //the difference between UTC and EST
var hrs = 18-(now.getHours()+diff); //18 is the target hour
any way of making the show/hide work for the specific time?
Date() objects are already UTC, when you use d.getDay() or d.getHours(), the local timezone is applied on the fly.
You just have to use d.getUTCDay(), d.getUTCHours(), etc. to prevent this

Categories

Resources