Reputation: 77
I am banging my head to the wall trying to figure out why this code won't run. The function isWorkingHour is sure working because I am using it here: Working Hours (Working Demo)
Can anyone help. Here is the JS code:
function makecall()
{
if (isWorkingHour(now)) {
//it's in schedule
window.alert("Office is open");
}
else {
window.alert("Office is close");
}
}
function isWorkingHour(now) {
return now.getDay() <= 4 && now.getHours() >= 9 && now.getHours() < 17;
}
The HTML I am using is as follows:
<input type="button" id="CallButton" class="callButton" value="Call" onclick="makecall()" />
<p id="demo">Click the button to check if now is working hours</p>
Here is the JSFiddle of what I am stuck with. http://jsfiddle.net/zfSTj/3
Upvotes: 2
Views: 248
Reputation: 9316
makecall = function() {
if (isWorkingHour(new Date())) {
//it's in schedule
document.getElementById("demo").innerHTML = "Office is open";
} else {
document.getElementById("demo").innerHTML = "Office is closed";
}
}
Do this to make makecall
a global function so you can access it within JSFiddle.
I also went ahead and replaced now
in your original parameter (which doesn't exist at the time of it being passed) with a new Date()
Upvotes: 1
Reputation: 1442
In the JSFiddle is pretty clear: the now
variable is not defined. Replace now
with new Date()
.
Upvotes: 1
Reputation: 944568
First, makecall
isn't a global function (it is wrapped in function that is called onload), so you can't access it from an intrinsic event attribute.
Second, you never define "now".
Start by defining now
:
function makecall() {
var now = new Date();
Then attach an event listener instead of using an onclick
attribute:
document.getElementById('CallButton').addEventListener('click', makecall);
Such: http://jsfiddle.net/zfSTj/8/
Upvotes: 1