How to set interval (time) of to execute javascript code? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to set interval in javascript.
I currently have javascript code whose execution is delayed, I need that code to be executed every x seconds. Is there any way to do that?
var delayInMilliseconds = 3000; //1 second
setTimeout(function() {
//MY CODE HERE
}, delayInMilliseconds);
Thank you

You want to use setInterval. It will execute the function every x milliseconds.
const delayInMilliseconds = 3000;
setInterval(() => {
// Your code here
}, delayInMilliseconds);

you can use setInterval() function:
var delayInMilliseconds = 3000; //1 second
setInterval(function() {
// do stuff
}, delayInMilliseconds);

Related

why the setInterval method gets 10 as input variable? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
my stopwatch with javascript and I don't get this point
buttonStart.addEventListener("click", () => {
if (Interval) {
clearInterval(Interval); }
Interval = setInterval(startTimer, 10); });
The second value it receives is the number of milliseconds between repetitions. Meaning each 10 milliseconds it will run the startTimer function.

Change inner html every x seconds [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
how to change itens in a table with javascript, every 15 seconds using uinnerHTML:
document.getElementById("iten1").innerHTML = "Bla Bla Bla";
You can create a recursive timeout loop.
function timeout() {
setTimeout(function () {
// Do Something Here
// Then recall the parent function to
// create a recursive loop.
timeout();
}, 1000);
}
If you're new to setTimeout() MDN has a pretty straightforward guide you can play around with. : setTimeout MDN

How can I solve this problem with the screen touches? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like you to tell me if there is an event that returns to me when it was the last time a user touched the screen, since I need a function to run 3 seconds after the user touched the screen for the last time
There's no specific event for your use-case. What you can do however is adding a click event listener to your window object and assign the current time to a global variable as soon as someone clicked the screen. Additionally you have to use JavaScript's setInterval() method, which executes a function periodically e.g. every 20 ms. Inside it's callback function you can compare the current time to the time stored in the global variable and if the difference is bigger than 3000 trigger your action.
Here's an example (just click 'Run code snippet'):
var timeClicked;
function screenClicked() {
timeClicked = new Date();
}
function check() {
if (timeClicked != null) {
if (new Date() - timeClicked > 3000) {
clearInterval(interval);
alert("time's up!");
}
}
}
window.addEventListener("click", screenClicked);
var interval = setInterval(check, 20);
<body bgcolor="#eeeeee"><span>click anywhere inside this window</span></body>

Loop with setTimeout with different delays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have to make a kind of playlist of events, where each event has a specific play time and the next one has to play after the time's up and so on.
Currently I'm using the setTimeout function. Now I would need to loop to get my events and times.
My code:
for(let i = 0; i < playlist.length ; i++){
setTimeout(function(){
$('#ventana').attr({src: playlist[i].url_play});
}, playlist[i].time_play*i);
}
But the timing is not right. How can I correct this?
It will indeed not work if you set the delay to playlist[i].time_play*i: think of it... It does not take into account the delays for past playlists, only the current one -- multiplied by its index(??).
Instead you would need to accumulate the different delays, for instance in a delay variable:
for (let i = 0, delay = 0; i < playlist.length ; delay += playlist[i].time_play, i++) {
setTimeout(function() {
$('#ventana').attr({src: playlist[i].url_play});
}, delay);
}

How to make setInterval work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So here is my code and I want to add 1 to total every second`
var doughnut = 0;
function myFunction(){
document.getElementById("total").innerHTML = " total: " + setInterval(doughnut +1, 1000) ;
}
Can you explain me how setInterval() works and where to put it?
setInterval accepts two arguments:
The "What": The function to execute
The "When": interval time in milliseconds to execute that function
Basically each second (1000 ms) the function increments the doughnut value and writes the updated value to the HTML element with id total.
Try this:
var doughnut = 0;
setInterval(function () {
doughnut++;
document.getElementById("total").innerHTML = " total: " + doughnut;
}, 1000);

Categories

Resources