How to make setInterval work? [closed] - javascript

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);

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.

How to set interval (time) of to execute javascript code? [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
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);

How to calculate savings from given value with function? [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
Should I calculate a savings of 15 % given an amount x?
function main(){
var salaryAmount = parseInt(readLine(), 10);
// Complete the function call
getSaving();
}
// Complete the function
function getSaving(){
};
If I understand the problem correctly, you are trying to create a function which tells you how much you would need to save in order to be saving 15% of any given salary.
function main(){
var salaryAmount = parseInt(readLine(),10);
var saving = getSaving(salaryAmount); // we pass the salary to the function
}
function getSaving(salary){ // we add a parameter for the salary
return salary * 0.15; // we multiply the salary by 15% and return that value
};
we could then simplify this code and remove the second function altogether.
function main(){
var salaryAmount = parseInt(readLine(),10);
var saving = salaryAmount * 0.15; // we calculate the savings here
}
Hope this is the answer you were looking for!

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

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);
}

Categories

Resources