Reputation: 71
I have a function that triggers an alert at a given time:
function breakTime() { // <<< do not edit or remove this line!
/* Set Break Hour in 24hr Notation */
var breakHour = 11
/* Set Break Minute */
var breakMinute = 47
/* Set Break Message */
var breakMessage = "Reports! Take Your Break!"
///////////////////No Need to Edit//////////////
var theDate = new Date()
if (Math.abs(theDate.getHours()) == breakHour && Math.abs(theDate.getMinutes()) == breakMinute) {
this.focus();
clearInterval(breakInt)
alert(breakMessage)
}
}
var breakInt = setInterval("breakTime()", 1000)
I want to assign this function to a button, so when the button is pressed, at an interval of 10 seconds after button press, the alert shows up to everyone that has access to the page. Is there any way to do it?
Note: the pop up alert is triggered without the button, but every one of my attempts to make a button that triggers the function doesn't trigger the pop up on every one's PC.
Upvotes: 2
Views: 3499
Reputation: 1178
You can assign function on button click like
<input id='btn' type='button' onclick='breakTime();' >Click</input>
OR You can fire event on click
<input id='btn' type='button' >Click</input>
<script>
$(document).ready(function(){
$('#btn').onclick(funcion(){
breakTime();
});
})
</script>
Upvotes: 1
Reputation: 6169
using just javascript:
<button id='buttonID'>Click Me</button>
function test() {
alert('break time!');
}
document.getElementById('buttonID').onclick = test;
// no () on test, otherwise it runs immediately.
https://jsfiddle.net/axq0ks6b/
HOWEVER this will only work locally on your page. If you want to press a button and send a prompt to everyone who has the page open, then you will need to set up server side scripts.
Upvotes: 1