Increase number in function and onclick - javascript

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

Related

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

Error in JavaScript closure

I have a modal dialog with a submit button, which when clicked caused the following code to execute:
$("#addqueuebutton").on("click",function(){
var counter = 0;
return function(){
counter += 1;
...
alert(counter);
};
});
I have to ensure that the counter is initialised only once, and so I tried to use a closure. But the code is not executing properly as the alert box does not appear. I tested the code on online compilers and it appears to be correct. Can someone tell me if there is something wrong in my syntax?
You're assigning the wrong function to click. When you click, you initialise counter and then return the inner function.
You need to call the outer function and assign its return value to the second argument of on().
}); should be }());
Or, to make it clearer:
function create_counter(){
var counter = 0;
return function(){
counter += 1;
...
alert(counter);
};
}
var counter_incrementing_function = create_counter()
$("#addqueuebutton").on("click", counter_incrementing_function);

Javascript function call into loop each time

I have function called rotator(id): this function animate div and I can called this function with different id for animate different elements
Actually I use 5 differents id , 1,2,3,4,5
And for call I need put :
rotador(1);rotador(2);rotador(3);rotador(4);rotador(5);
The problem it´s that I want to rotate in automatic mode. For this I think to use this
for (i=0;i<=5;i++) {
setTimeout(rotador(i),2000);
}
But it doesn't work because it animates all in the same time, no let firt execute the first and continue before of first go second , etc , etc and when go the end or number 5 start other time in one
My problem it´s this if you can help me THANKS !!! :) Regards
You are actually calling the rodator(i) function, and schedule for execution after 2 seconds the result of the rodator. In other words, your code is now equalent to:
for (i=0;i<=5;i++) {
var result = rotador(i);
setTimeout(result,2000);
}
You can accomplish this either by creating a function for the callback:
for (i=0;i<=5;i++) {
setTimeout((function(i){
return function(){
rotador(i);
}
})(i),2000 * i);
}
or you can call the next rodator in the rotador function itself:
var rotador = function(i){
// your code
if (i < 5) {
setTimeout(function(){rotaror(i + 1);}, 2000);
}
}
Note: the closure in the second example is needed to call the function with the correct value of i. We are creating an anonymous function, and create i as a local scope variable, which value won't be mutated by the outerscope changes. (we can rename i to n in the local scope, if this would be more readable). Otherwise the value of i will be 5 each time rotador is called, as the value of i would be modified before the actual function call.
since setTimeout() does not wait for the function to be executed before continuing, you have to set the delay to a different value for different items, something like 2000 * (i + 1) instead of just 2000
EDIT: yes, and you need the callback as Darhazer suggests
rotationStep(1);
function rotador(id)
{
console.log(id);
}
function rotationStep( currentId )
{
rotador(currentId);
var nextId = currentId<5 ? currentId+1 : 1;
setTimeout(function(){ rotationStep(nextId) },2000); //anonymous function is a way to pass parameter in IE
}
Use a callback:
setTimeout(function() {
rotador(i)
}, 2000)

Why is my javascript for loop not working?

fourI am writing a javascript for loop and am sure have done a terrible job:
init = function () {
var i = 0;
timer = setInterval(function () {
if (i >= 4) {
clearInterval(timer);
return;
}
for (i = 0; i < 10; i++) {
console.log('init fired');
}
}, 2000);
};
init();
What I want is for the timer to stop after the i variable in the for loop reaches four. Instead the log is showing init fired ten times. What am I doing wrong?
I think you need it like this
var i=0; //Global Declaration
init = function(){
timer = setInterval(function(){
console.log('init fired');
i++;
if(i>4){
clearInterval(timer);
return; }
}, 2000);
};
init();
Hope this solves your problem. This will trigger init() method four times as you have expected and if the i reaches 4 the interval will be cleared.
Every time the timeout handler runs, it starts "i" back at zero.
The problem with your "for" loop is basically that you should not use a "for" loop :-)
Those 10 iterations are happening on the first pass through the function. After that first pass, "i" will be 10 and so the "if" condition will cancel the timeout. However, that "if" check is only made at the beginning of the function, so the loop will always complete 10 iterations.
If you want to have just four iterations of the timer (or five or whatever), you'd just leave off the "for" loop, and add i++; after the console log message. That way, the timer would issue one log output when it runs, and when that's happened the number of times you desire, it will stop.

jQuery increment setInverval()

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

Categories

Resources