jQuery increment setInverval() - javascript

Uh..... I have no idea why this simple code snippet isn't working:
function increment() {
var count = 0;
alert(count++);
}
setInterval(increment, 1000); // test
it should display the next number repeatedly, right? It just displays "0". Help appreciated. Thanks.

count needs to be a global variable. You are referencing your local variable count. Every time the function executes, it gets set to 0. You are using count++, so the value passed to alert is 0, and the incremented value is never used.
To fix this, move the declaration of count outside of your function (as per answer by Tim):
var count = 0;
function increment() {
alert(count++);
}
setInterval(increment, 1000); // test

var count = 0;
function increment() {
   alert(count++);
   }
 setInterval(increment, 1000); // test

Related

counter increase each time loads the function

I am pretty new into javascript , but I am trying to create a counter , but when I setup my counter . it looks gives me the same value , but I want first round 1 then 2 then 3 to n-1
function timer(){
var contactsNum = 0;
contactsNum++;
console.log(contactsNum)
}
(function loop() {
setTimeout(function () {
timer()
loop()
}, 9000);
}());
This is because variable contactsNum is local to the function timer, and every subsequent call of function timer will initialize the value 0. If you want to keep track of the value you can use closure.
The problem is here:
function timer(){
var contactsNum = 0;
contactsNum++;
console.log(contactsNum)
}
You are defining and assigning a variable in timer. That means, everytime timer gets called, contactsNum gets set to 0.
You need to put it outside of the function like:
let contactsNum = 0;
function timer() {
contactsNum++;
console.log(contactsNum)
}

setInterval call to function with other arg

I have a function called "showCustomer" that get number between 1-5 and return something.
I want to use setInterval, to run this function every 5 second but with another number.
Its not working, i don't understand why its not working to me. here is the code.
setInterval(function () {
var i = 1;
showCustomer(i);
i++;
}, 5000);
Just move the declaration of variable i before the setInterval() call:
var i = 1;
setInterval(function () {
showCustomer(i);
i++;
}, 5000);
The anonymous function you've set as a callback for setInterval gets called every 5 seconds in your code. In every call, you're setting i to 1 which resets it every time.
Moving i outside the setInterval callback makes it persist the the current value.
Every time you use var, you redeclare the value of that variable. So you only should declare the counter one time.
Every time that the browser calls the callback showCustomer the if statement evaluates if the browser should make a new call.
clearInvertal() it's the method to stop the setInterval() method.
var id = 1;
var show5times = window.setInterval(showCustomer, 5000);
function showCustomer() {
alert(id);
id++;
if(id > 5) {
window.clearInterval(show5times);
}
}

Set Value of Variable on first instance of function running

For example I have a variable which should increment every time the function is run. I want the variable to initially be given the value of 0, but then increase by 1 every time the function is called. I do not want to give the variable global scope, however obviously if I declare it within the function it is going to be reset to 0 every time it is called.
Is there a simple and efficient way to to do this in Javascript?
My Javascript:
function myFunction(){
var i=0;
//Body of function
i++;
}
You can set the value on the function object itself, like this
function myFunction() {
myFunction.i = myFunction.i || 0;
myFunction.i++;
}
Alternatively, you can use closure, like this
var myFunction = (function () {
var i = 0;
return function () {
// `myFunction` will be this function only and it increments the
// `i` from the enclosed function.
i++;
}
})();

Increase number in function and onclick

I have problem for increase number in one unit when i call function and onclick event
I have this:
function caller() {
counter = 0;
alert("ok" + counter);
jQuery(".db_header_general" + number).click(function() {
alert("ok" + counter);
counter++;
});
}
In this case if i call the function always the alert tell me "0" and if i call function and use the event onclick yes the result it´s right 1 ,2 ,3 ,4 , etc , the problem it´s if i go and activate the function no continue from the last counter number and always show "0"
I need for example call function and show 1 ,2 ,3 and if event click it´s activate continue and add +1 from the last counter number , but the problem - and i can´t get works - . it´s no continue with the last number
Also i try this
function caller() {
counter=0;
alert("ok"+counter);
jQuery(".db_header_general"+number).click(function() {
alert("ok"+counter);
});
counter++;
}
Using counter outside of click event, but also the same result and no works for me
Thank´s and the best regards for the help
You should place counter outside the function. With current functionality when ever you call the function the counter value sets to 0. Do it like this
var counter = 0;
function caller ()
{
// functionality
counter++;
}
counter=0
gets executed every time you call your function because it's a local variable. So every time you call your function, you set the counter to 0. Make counter a global variable and it'll work.
Example added:
<script type="text/javascript">
var counter = 0;
function caller(){
alert("ok"+counter);
jQuery(".myBtn").click(function() {
alert("ok"+counter);
counter++;
});
}

Is it possible to call a clearInterval method on a setInterval variable that is out of scope?

I have a setInterval function that is initializing a varibale with it's id in a for loop, which results in a number of setInterval functions executing in the for loop. Each of the setInterval functions will be assigned to a variable now my question is, is it possible for one single variable to contain the values of all of the setIntervals id's or only one? If all of the id's can all be contained in a single variable is it possible to clear certain setIntervals via there id that is contained in the variable or will I need to declare a unique variable for each setInterval to be able to do this?
var intervalId;
for(var i = 0; i < 10; i++) {
intervalId = setInterval(function() {}, 100);
}
The values returned from setInterval and setTimeout are numbers, so only one will "fit" in a variable.
You could make an array:
var intervalId = [];
for(var i = 0; i < 10; i++) {
intervalId.push( setInterval(function() { ... }, 100) );
}
Note that there's something important missing from your question, and that's the code in the interval function. Specifically, if that code wants to refer to "i", there's a problem: all of the separate functions will share the same "i" variable (the one declared in the loop header).
To get around that, you could do this:
function makeTimerFunction(i) {
return function() {
// the code
if (whatever) { clearInterval(intervalId[i]); }
// and so on
};
}
var intervalId = [];
for (var i = 0; i < 10; i++)
intervalId.push( setInterval( makeTimerFunction(i), 100 ) );
By using a separate function, you create another scope and can "freeze" each value of "i" in a separate, per-timer variable.
edit — #pst points out correctly that if it is really the case that you do want to refer to the timer id from inside the handler functions, then there's really no need for that "intervalId" array at all - you could just use the wrapper function to isolate the call to setInterval().
function createTimer() {
var timerId = setInterval(function() {
// the code
if (whatever) { clearInterval( timerId ); }
// ...
};
}
for (var i = 0; i < 10; ++i)
createTimer();
That way each timer is set up with it's own private timer ID variable. Of course, if you do need the timer id outside the function, then you need an external cache for the ids.

Categories

Resources