1

I am increasing value of a variable inside a timer.It is also inside a button onClick event.

setInterval(function () {
    document.getElementById('z_in').addEventListener('click', function () {
        zoom_level = 1;
    }, false);
    document.getElementById('z_out').addEventListener('click', function () {
        zoom_level = 3;
    }, false);
    document.getElementById('rotate').addEventListener('click', function () {
        angle = angle + 10;

    }, false);
    Find(db, minLon, maxLon, minLat, maxLat, zoom_level, angle, stage, layer, i);
    i++;
    //angle = 0;
    console.log();
}, 1000);

Here angle increases too much not ten by ten in every loop.What is the reason of this problem?

5
  • not enough code. can you make a fiddle... Commented Sep 19, 2013 at 8:49
  • You add an additional eventlistener each second. Thus angle increases by ten for every eventlistener added. Commented Sep 19, 2013 at 8:53
  • An offtopic question: which facts had spoken for it to pass complete javascript functions as parameters? In my eyes it is bad practise and makes it harder to read forign code. Commented Sep 19, 2013 at 8:53
  • jsfiddle.net/PPzQv you can see angle values in console.It does not increases ten by ten. Commented Sep 19, 2013 at 8:55
  • @C5H8NNaO4 So what can i do to handle it? Commented Sep 19, 2013 at 9:18

1 Answer 1

2

If you need listeners only when timer is active, you can use something like this:

var timer, i = 0, zoom_level = 1, angle = 0;
var z_in_el = document.getElementById('z_in');
var z_out_el = document.getElementById('z_out');
var rotate_el = document.getElementById('rotate');

function zoomIn(){
    zoom_level = 1;
}
function zoomOut(){
    zoom_level = 3;
}
function rotate(){
    angle += 10;
}

function startTimer(){
    addListeners();
    timer = window.setInterval(function(){
        i++;
        Find(db, minLon, maxLon, minLat, maxLat, zoom_level, angle, stage, layer, i);
        console.log(i, zoom_level, angle);
    }, 1000); 
}

function stopTimer(){
    removeListeners();
    window.clearInterval(timer);
}

function addListeners(){
    z_in_el.addEventListener("click", zoomIn, false);
    z_out_el.addEventListener("click", zoomOut, false);
    rotate_el.addEventListener("click", rotate, false);
}

function removeListeners(){
    z_in_el.removeEventListener("click", zoomIn, false);
    z_out_el.removeEventListener("click", zoomOut, false);
    rotate_el.removeEventListener("click", rotate, false);
}

Use startTimer() to set interval or stopTimer() to remove it.

Sign up to request clarification or add additional context in comments.

2 Comments

Fixed answer. Is it what you need?
Yep.Thank you so much Anton.That would be so very useful for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.