-2

I've made a function which gets invoked each time when values of my sliders were being changed. The problem consists in that this function generates an array which later it passes to other functions. So far, so good but sliders may be adjusted аs many times the user wants to and as I said, this triggers the function which generates an array, which means that I can get hundreds or even thousands of arrays while I need actually the last one. Here is my code:

function tg() {

    $(".dot").remove();

    var p, x, y, xo, yo, agl, spd, g, t = 1, isin2, angle1, n = 0, i = 1;
    var m = [];

    p = $("#ba").position();
    xo = p.left;
    yo = p.top;

    spd = $("#spd").val();
    agl = $("#agl").val();
    g = $("#g").val();
    angle1 = formula;

    isin2 = true;

    while (isin2) {
        y = formula;
        x = formula;

        m.push([x, y]);

        //...

        t++;
        isin2 = isin(x, y);

    }
    if (isin2 == false) {
        $(".dot").last().remove();
        m.pop([x, y]);
        console.log(m);
    }
    activate(m);
}

There come the sliders, I've actually given just one of them because the rest two are absolute copies, they're just addressing other variables.

$(document).ready(function(){
    $("#slagl").slider({
        min:0,
        max:89,
        value:15,
        range:"min",
        slide: function(event, ui){
            $("#agl").val(ui.value); 
            tg();
        }
    });

That's the what console.log() returns for m

As you can easily see it just piles new and new arrays without cleaning the others. The goal is to delete somehow all the others redundant arrays, made on slide and leave only the last one, each time the function is called.

5
  • m.push([x,y]); if you need only one element, than apply it direct to m, or to the first element, or just keep the last 10 or so ... Aug 22, 2015 at 18:35
  • No, I need bunch of elements stored in the array but not bunch of arrays with bunch of elements put in them, as it happens. Even the slightest slide of the slider appends 5-6 new arrays. I guess, browser retains them somewhere but I need to find where this place is and how to access it.
    – KDX2
    Aug 22, 2015 at 18:37
  • hint setTimeout Aug 22, 2015 at 18:46
  • "Even the slightest slide of the slider appends 5-6 new arrays" - it sounds like you need a debounce - stackoverflow.com/q/24004791/1837472
    – Data
    Aug 22, 2015 at 18:48
  • @Felipe Brahm, yeah but the problem is that this algorithm passes each of those arrays to the following function while it needs to get only the last one, which makes my ball run trough 1000, figuratively said, coordinates until it reaches the correct (last) array containing the correct ones. That's what makes me think the best place to idk, remove or blocks the redundant functions should happen in tg(), as it generates and packages and sends the needed values.
    – KDX2
    Aug 22, 2015 at 18:52

3 Answers 3

1

In a private conversation with the OP we found out that the arrays were in fact properly generated. The problem laid in activate(m);, which would get called every time a slider has changed. In activate(), a click event is assigned to a button, which resulted in piling of redundant click events on every update. Thus, whenever the button was clicked, it would execute its code with older values of the array, too.

0

Please use slider's 'change' function. This gets called only once when user releases the slider's handlers, as compared to 'slide' callback which gets called on each pixel move.

5
  • You could improve your answer with some links and reference snippets. Aug 22, 2015 at 18:52
  • That's one of the things which I thought to do as I completely agree that this would reduce drastically the arrays but however the previous ones will still be contained somewhere in the browser/program and I need to access them and get rid of them. Nice, suggestion though, I'll right now apply it. :) We're close..
    – KDX2
    Aug 22, 2015 at 18:56
  • * this reduced on 90% the amount of arrays, successfully, now we have to delete the last one. I'll try to explain it more accurately, those arrays are the trajectory of points through which my 'ball' has to go. On each slide I create new trajectory of such points and when I do >1 new ones the ball starts to follow the first one ever created, then the second one until it reaches the last one, which makes it, well buggy :D
    – KDX2
    Aug 22, 2015 at 19:01
  • If those arrays are private and defined inside tg() function then you really need not to worry about memory leaks. Those will automatically garbage collected as soon as program execution comes out of that tg() function. Just to cross check it; try putting 'm' in your browser console and see if you get any value printed out there or not. Thanks.
    – vijayP
    Aug 22, 2015 at 19:04
  • hmm, that sounds strange, then maybe it is a logical error... See what happened when I set slide: to change:... just compare this picture to the one above, where I posted the topic... [IMG]i60.tinypic.com/2wqrjo9.png[/IMG] That's after 3 adjustments, two from the slider + the initial one. The problem is still there guys, it continues piling arrays.
    – KDX2
    Aug 22, 2015 at 19:14
0

You should either use the slider change function or use some debouncing:

$(document).ready(function(){
  var timer;
  $("#slagl").slider({
    min:0,
    max:89,
    value:15,
    range:"min",
    slide: function(event, ui){
      $("#agl").val(ui.value);
      if(timer) {
        clearTimeout(timer);
      }
      timer = setTimeout(tg, 500);
    }
  });
});

Change the value 500 to whatever works better for your use case.

6
  • yeah, this helped me a lot, as it reduced much of the arrays but it's still saving the previously generated array somewhere and on change of the slider I need to get rid of the previously calculated array and place the new one with the new values obtained from the slider on its place.
    – KDX2
    Aug 22, 2015 at 19:06
  • What do you mean by it's still saving the previously generated array somewhere? The array is not saved anywhere. It will be automatically removed by the GC. Can you please explain what you mean? Aug 22, 2015 at 19:09
  • Ok, I have sliders and with them I manipulate vars ang, spd and g with which I calculate x=.., y=... Because this process is looped I'm just pushing the newly calculated x and y as they're functions, a mathematical one, of time (t). When I finish sorting out the x and y-s I get an array which appears to be the 'path' of my projectile and which I'm going to send to a function realizing the 'movement' along this path. From what I see, I can tell you that if I adjust one or two or all of the sliders and i hit 'play' the ball starts following the first path made, then the second and continues
    – KDX2
    Aug 22, 2015 at 19:23
  • tracking all the trajectories built in the sequence of the adjustments from the sliders. I want to place the ball, adjust its values from sliders and when click play it to follow just one single path, not to start with all the others and eventually to reach the one I've just set...
    – KDX2
    Aug 22, 2015 at 19:27
  • The what you tell me means that each time I call a function, regardless of how many times I did it before the compiler or whatever it is will clean all the previously used arrays, in my case and will create the new one, as if I'm running the script for first time, if I'm getting it right. If it's like you say, which is good news, then what... Those sliders call tg() on each change, then why the movement() will read all the previous slider adjustments when the CG is supposed to have cleaned them? If youve even just ideas, not examples I'd be glad to read them, I'm going to think also on this :)
    – KDX2
    Aug 22, 2015 at 20:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.