views:

57

answers:

3

I have two functions

function1()
{
     //...
}

in function1 i make ajax requests and load an image

there is another function - function2 where i call function1 to load an image.

now, what is the question.

How can i stop propagation of function1 before i will call it again in function2?

function2()
    {
        // ...
        // how stop old propogation of function1 here?
        function1(); //call function1 again

    }

Thanks

A: 

Umm I don't really get what you are trying to do here but still......

If you are trying to cancel AJAX request, I don't think there is an easy solution. It would be better to initiate another request to reverse/stop the changes.

But as you are asking "How to stop a function?", my answer is why initiate it at a place where its not needed. Call it where required.

If you really want to initiate it & run on specific event, set an if block inside the function. Or rather in your case set a flag in function 1 & change it from function 2.

loxxy
the problem is in following: in `function2` on `mousemoove` event of some element i call `function1` to load an image. But if i moove mouse for a long time(ie call function1 many many times) as result i don't get the finally image(i get an image from some current state), so i want to stop propogetin of early callings of function...
Syom
A: 

Hi,

Use a flag which you can manipulate in wider scope. Like:

flag = false;

function function2(){
    if(flag) function1();
}

now you can call

function2(); // and function1 will not be called

or set:

flag = true;

function2(); //now function1() will be called.
Bandpay
:) i need inverse variant. a need to call `function1` anyway. but before stop it's working from another calls before..
Syom
A: 

Rather than load the image immediately func1() is called, you can insert a little delay; that way, you can cancel the previous download and schedule a new one each time the function is called. Here's an example:

var delay = 3500, // Delay for 3.5 seconds
    timeoutId;

function func1(url) {
    if (timeoutId) clearTimeout(timeoutId);
    setTimeout(function() {
        // Add code to download image here
    }, delay);
}

function func2() {
    // As long as you call func1 again before the timeout elapses, the
    // previous download should be cancelled, leaving you with the last
    // selected image
}
elo80ka