How do I send alerts every minute? [duplicate] - javascript

This question already has answers here:
JavaScript: get code to run every minute
(3 answers)
Closed 5 years ago.
I'm new to html and javascript but I was wondering how I could go about sending alerts like this https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_alert every minute or so.

setInterval(() => {
// your code
}, 1000)
https://www.w3schools.com/jsref/met_win_setinterval.asp

javascript have a method to repeat something like a task every so often, this method is the function setInterval this function repeat that you want.
Javascript:
setInterval(function(){
alert("Hello");
}, 3000);
For example this method shown every 3 seconds an alert that say "Hello", if you want repeat a task every 3 minutures you need change 3000 to 180000 like this:
setInterval(function(){
alert("Hello");
}, 180000); // repeat every 3 minutes
Here is the docs of the function.
I hope it helps you, regards!

Related

MomentJS on Angular to keep changing value on realtime [duplicate]

This question already has answers here:
Angular 6 run a function in every X seconds
(5 answers)
Closed 2 years ago.
I have an array of objects that has a property called startedTimestamp. I need to show on the front-end how long ago that has been running, on this case I'm using MomentJS with the fromNow() method. However, that will not updating as the time goes by... to something like 1 minutes ago... and then 2 minutes ago... and so on. Is there a way around that?
I'm using Angular.
Thanks
I cannot recreate an angular scenario here but it seems setInterval will do the trick. It's important to create the moment object just once (outside setInterval) then the label's text will be updated every second.
Run the code and wait at least a minute and you'll see how it changes from 'a few seconds ago' to 'a minute ago'
const label = document.querySelector('label'),
span = document.querySelector('span');
const m = moment();
let secondsPast = 0;
setInterval(()=>{
span.textContent = `${++secondsPast} seconds past`
label.textContent = `${m.fromNow()} from ${m.format()}`
},1000)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
<span></span><br><br>
<label></label>

setInterval time not precise [duplicate]

This question already has answers here:
How to create an accurate timer in javascript?
(15 answers)
Closed 5 years ago.
I'm developping an ionic app.
I create a counter with setInterval.
let test = new Date().getTime();
setInterval(() => {
console.log(new Date().getTime() - test);
test = new Date().getTime();
}, 1000);
Problem, the console.log give not the answer 1000. It is completely random and sometimes more thant 3000.
Have you an idea why is it so?
There is no way to guarantee the exact time because even the simplest function like this it takes some millisecond to execute the content inside the interval function. setInterval function only guarantee the time interval.

How would I make a stopwatch in Javascript/jQuery? [duplicate]

This question already has answers here:
How to create a stopwatch using JavaScript?
(7 answers)
Closed 8 years ago.
How would I make a stopwatch in Javascript/jQuery?
I have developed a few methods of my own, here is one using while loops. This stopwatch is merely meant to count for a minute.
function myStopwatch() {
var $count = 0;
while($count < 60) {
$count++;
}
$count.delay(1000); //makes $count one second long
}
myStopwatch()
Using setInterval() may be better idea:
var count=0;
var timer = setInterval(function(){
if(count<60) count++;
else clearInterval(timer);
},3000);
Use setInterval...
var count = 0;
doEverySecond(){
// something to do every second...
count++;
if(count > 60) clearInterval(timer);
}
var timer = setInterval(doEverySecond, 1000)
jQuery's .delay() does not halt the execution of javascript like you're trying to do. It only works with asychronous operations that use the jQuery queue system such as animations which means it will do nothing in your current code since you are not using any jQuery queued operations.
In javascript, the way that you "delay" for one second is to use setTimeout() or setInterval() and specify the callback function that you want called at some future time from now.
setTimeout(function() {
// this code here will execute one second later
}, 1000);
// this code here executes immediately, there is no delay
var x = 1;
So, if you want to wait for something for a minute, you would do this:
// execute some code one minute from now
setTimeout(function() {
// this code here will execute one second later
}, 1000*60);

Run JavaScript function at regular time interval

I am currently building a website to host software. What I want is to add to the project pages is a slideshow of screenshots cycling, changing images about every 5 seconds. Is there any way to a script triggered at a time interval using just JavaScript? ..or will I have to resort to alternative methods for achieving my desired functionality.
Thanks in advance for any help!
setInterval:
function doSomething() {
alert('This pops up every 5 seconds and is annoying!');
}
setInterval(doSomething, 5000); // Time in milliseconds
Pass it the function you want called repeatedly every n milliseconds. (setTimeout, by the way, will call a function with a timeout.)
If you ever want to stop the timer, hold onto setInterval’s return value and pass it to clearInterval.
You want the setInterval function.
setInterval(function() {
// This will be executed every 5 seconds
}, 5000); // 5000 milliseconds
Basic reference: http://www.w3schools.com/jsref/met_win_setinterval.asp (please ignore the reference to the "lang" parameter)
More indepth reference: https://developer.mozilla.org/en-US/docs/Web/API/window.setInterval
You can use window.setInterval
Sample usage:
window.setInterval(function () {
console.log("foo");
}, 3000);
It Changes the date time in a div and time changes frequently after 1 sec.
setInterval(function(){
var date=new Date();
$('.class').html(date);
},1000);

Create blinking "text" in javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Text blinking jQuery
I'm trying to create a timer in my HTML5/JavaScript game when the game is over. Something like 3...2...1.. where 3 will appear then flash, then 2, then flash, then 1, then flash and return back to my title screen... Nothing I put will get that to work... Anybody able to help me out here? So far my code for the numbers is this:
function CreateTimer(){
timer = setInterval(function() {
cntxt.fillText(time, c.width/2 - cntxt.measureText(time).width/2, c.height/4);
time--;
}, 1000);
}
function resetTimer(){
clearInterval(timer);
time = 3;
where = "menu";
}
But even this will just place the 2nd number and then 3rd number straight over the previous and then return the title as expected.
Thanks in advanced!
You need to clear the text first see http://www.w3schools.com/html5/canvas_clearrect.asp

Categories