Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Function setInterval() works only one time

Guys! I wanna ask about Javascript function setInterval(). My problem is that setInterval() works only one time, not repeating.

Here is my HTML Code

<button id = 'btun' name = 'btun' onclick = 'changecolor();' class = 'btn btn-success btn-block'>Color Change</button>

and Javascript Code

function below(t){
  var button = document.getElementById('btun');
  var quadrant = (t*t + 2*t + 1)+"px";
  console.log('ye');
  button.style.marginTop = quadrant;
  document.write(pix);
}
var doBelow = setInterval(below(t++),1);
if(t > 50){
  clearInterval(doBelow);
}

I can't find what is wrong.

like image 708
HyeonJunOh Avatar asked Dec 30 '14 02:12

HyeonJunOh


People also ask

How many times does setInterval run?

When your code calls the function repeatEverySecond it will run setInterval . setInterval will run the function sendMessage every second (1000 ms).

Does the setInterval () function work in JavaScript?

JavaScript setInterval() method. The setInterval() method in JavaScript is used to repeat a specified function at every given time-interval. It evaluates an expression or calls a function at given intervals. This method continues the calling of function until the window is closed or the clearInterval() method is called ...

How does the setInterval () function work in?

setInterval() The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. This method returns an interval ID which uniquely identifies the interval, so you can remove it later by calling clearInterval() .

Does setInterval repeat?

Yes, setInterval repeats until you call clearInterval with the interval to stop.


2 Answers

setInterval expects a callback as first argument, but you are calling the actual function.

Call should be like below

 setInterval(function() {
      below(t++); }
  ,1);

So here you are creating an anonymous callback which will call your function below. And better put the exit condition t >= 50 inside below function

like image 104
Mritunjay Avatar answered Sep 22 '22 16:09

Mritunjay


The setInterval doesn't work even one time. The reason that the function is called once is that you are calling it when trying to use setInterval, and the return value from the function (which is undefined) is used in the setInterval call.

Use a function expression to create an interval that calls below(t++). You would put the code that checks the t > 50 condition inside the function, otherwise that will also only run once.

function below(t){
  var button = document.getElementById('btun');
  var quadrant = (t*t + 2*t + 1)+"px";
  console.log('ye');
  button.style.marginTop = quadrant;
  document.write(pix);
  if(t >= 50){
    clearInterval(doBelow);
  }
}

var doBelow = setInterval(function() { below(t++); },1);

Note: Using document.write in the interval isn't a good idea. As it runs after the page is completed, it will open a new page to write to which replaces the current page.

like image 41
Guffa Avatar answered Sep 21 '22 16:09

Guffa