Javascript file suddenly stopped working with no changes - javascript

So I got help on this code earlier and it was working before perfectly. However, upon opening VS Code again the code no longer responds with zero errors.
var runned = false;
var d = new Date();
if (d.getHours(5) == 5 && !runned) {
alert("Hello");
runned = true;
}
The code is simply supposed to get the current hour and return an alert.
I verified that the js file was connected to the HTML file with some other code and it worked.

var runned = false;
var d = new Date();
if (d.getHours() == 5 && !runned) {
alert("Hello");
runned = true;
}
getHours() doesn't take parameters
getHours uses the 24-hour clock model, i.e 0-23 so 5 is 5 am but 17 is 5pm
d.getHours() as used above without an argument makes it work in all use case scenarios

Related

Do something when time is above the set time

I am just a newbie. I am trying delete a span when the time is above 6:30 pm daily. Code below:
(function(){
var d = new Date();
d.setHours(18,30,0);
if (d<new Date().toLocaleString());{
$("span:contains('Material Receive')").remove()
return false;}})();
However it s not working. It is always removing, i.e 24x7.
getHours Method will fit perfect i think
var d = new Date();
if (1830 < d.getHours()+""+d.getMinutes()){
$("span:contains('Material Receive')").remove()
return false;
}
Try to not compare an object with a string. Use 2 numbers instead. And lose the stray semicolon.
if (d.getTime() < new Date().getTime()){...}
Above answers already explained for you. Anyway:
Getting current date and time in JavaScript
I didn't understand exactly what you want to do, I suppose to delete a span every day at 18:30, right?
In this case, when you create the date object, you have to access hours and minutes to check time so:
( function() {
var d = new Date();
if( ( d.getHours() == 18 ) && ( d.getMinutes() == 30 ) ) {
$("span:contains('Material Receive')").remove();
return false; //Useless in a self-invoking function
}
})();

I need a running timer and checker

I am trying to create a function/alg that executes a script depending on the time.
This won't work with PHP's 'sleep' function (I've nested loops with headers and tried).
here is the logic I am seeking to accomplish:
$time= time(); // checks time
if($time == 3'o'clock) {
do_something();
} else {
check_time();
and_keep_counting();
}
I need a continous clock ticking | which can run in the background
at the same time, continous checking of the clock.
I may have to go jquery?
This will not be very efficient, but will do the trick:
(function time(){
var t = new Date().toLocaleTimeString();
console.log(t);
if(t === "3:00:00 PM"){
// Do whatever you need to here
runStuff();
}
// Call the function again in 59 seconds
setTimeout(time, 59000);
}());
function runStuff(){
alert("Time's Up!");
}
I'm not entirely sure what you're asking, but you can do something like this with JavaScript...
var date = new Date(); which creates a date-time object
var hours = date.getHours(); which gets the hours in "military time" format, meaning 1 is 1:00AM and 13 is 1:00PM
If you want to check if it's three o'clock, you can do
if (hours == 15) {
someFunction();
} else {
doSomeOtherFunction();
}

CRM 2011 Timezone in OnChange Event

I'm in the process of customizing our CRM solution. Due to different timezone, sometimes due dates in Cases or Tasks are not consistent because the time defaults to midnight 12 am. So to different timezone the due date could be one day later or earlier.
In order to fix this we're setting the default time of the due date field (logical name = "followupby" ) to 7 PM. We're creating an OnChange event for the Due date field with Java script function as follow. In "Handler Properties" dialogue to set up the Library and Function name, I check the option to "Pass execution context as first parameter". Therefore in my function below it's receiving the 'context' parameter:
setCaseDueDate: function (context) {
var oField = context.getEventSource().getValue();
var sTmp = oField;
var hours =19;
var minutes = 0;
var seconds = 0;
if (typeof (oField) != "undefined" && oField != null) {
var newTime = new Date(sTmp.getFullYear(), sTmp.getMonth(), sTmp.getDate(), hours, minutes, seconds);
context.getEventSource().setValue(newTime);
}
},
However I keep getting an error that says "There was an error with this field's customized event. Error 'setCaseDueDate' is undefined."
Could it be that certain values in my function is undefined or is it probably not getting the 'context' parameter correctly?
Thanks, greatly appreciate your help.
***UPDATE**********
Nevermind this... I was putting my function with existing collection of Javascript functions and then I decided to put into a separate new webresource and it's working now... weird but works.
Thanks.
Your code to define a JavaScript function is wrong, try with
function setCaseDueDate(context) {
// rest of the code
}
and to change the date, it's not necessary the context, for example:
function setCaseDueDate() {
var fieldName = "followupby";
var sTmp = Xrm.Page.getAttribute(fieldName).getValue();
if (currentDate != null) {
var newTime = new Date(sTmp.getFullYear(), sTmp.getMonth(), sTmp.getDate(), 19, 0, 0);
Xrm.Page.getAttribute(fieldName).setValue(newTime);
}
}

Cannot print message according to time

I've three different times, two of them are in string forms (time1 and time2) and one from system date currDate. Next step according to the one of two above times I want to print messages when the system date reaches one of them. For this I've function callEachMinute that calls each minute to get system time (but here in code I did not include the whole procedure). Here is the current status of the code:
Script:
function callEachMinute() {
var currDate = new Date();
var time_1 = '8:30';
var time_2 = '23:00';
timeComparison(currDate, time_1, time_2)
}
function timeComparison(currTime, time1, time2) {
// Time 1 formatting
var t1 = new Date();
var parts1 = time1.split(":");
t1.setHours(parts1[0],parts1[1]);
// Iftor-Time formatting
var t2 = new Date();
var parts2 = timeI.split(":");
t2.setHours(parts2[0],parts2[1]);
/* Notification procedure */
if (currTime == t1) {
// Message for Time1
alert("Time 1 is reached");
}
if (currTime == t2) {
// Message for Time2
alert("Time 2 is reached");
}
}
Problem:
When the system time is reached one of times (time1 or time2) nothing happens. Any solution for this problem?
There are a few things that could be problematic here.
You set up a Date object then want to compare it to currTime:
if (currTime == t1) {
unfortunatley Javascript's == operator when applied to objects compares two objects to see if they are references to the same object, so even if currTime and t1 contained exactly the same time this check would evaluate to false since they are different instances. You could do this by converting to a string:
if (currTime.toString() == t1.toString) {
which would work if the string representations for each data work out the same.
However, a more straight forward approach might be to tackle this the other way around - extract the hours and minutes from currTime, build a string and compare that to your time strings. Something like:
// in timecomparison function
var hrs = currTime.getHours();
var mins = currTime.getMinutes();
var now = hrs+":"+mins
// now do comparisons
if (now == time1 ) {
....
}
and so on.

Javascript between Two Times

I'm currently trying to create a JavaScript which will display on our website when our shop is open, and when it is closed.
I basically want to create an if statement between two times, these being 8:30 and 5:30.
I'm currently doing the following, although it won't work as I effectively have two lots of 'minutes' defined which cancel each other out.
<script type="text/javascript">
var Digital=new Date();
var day=Digital.getDay();
var hours=Digital.getHours();
var minutes=Digital.getMinutes();
// Monday - Open //
if (day==1 && hours>=8 && minutes>=30 && day==1 && hours<=17 && minutes<=30)
document.write('Open today until 5:30pm');
</script>
Can anyone suggest a way of achieving what I am trying to do?
How about this:
if(day == 1 && hours*60+minutes >= 510 && hours*60+minutes <= 1050) {
// do stuff
}
With 8 * 60 + 30 = 510 and 17 * 60 + 30 = 1050.
One thing to note here is
new Date()
gets the local time from the client's clock. If the client's clock is set at a different time zone you might not get the result you are hoping for.
I would suggest getting the client's timezone as well and converting that to your desired timezone adding/subtracting any offset, start with something like
var clientTime = new Date();
var clientTimeZone = clientTime.getTimezoneOffset();
//getTimezoneOffset returns the time-zone offset in minutes between the current locale and UTC.

Categories

Resources