2

I have a function which calls itself with a pause of 2 seconds until the ajax call returns 0. Now it can go on for a long time, hence i wish to pause it or stop it with an external event like a button click.

function create_abcd()
{
    var dataString = 'action=create_abcd&type=' + $('#abcd_type').val() + '&count=100';
    $.ajax({
        type: "POST",
        url: "backend.php",
        data: dataString,
        success: function(msg){
            if(msg != "0")
            {
                $("#abcd_output").append('<p>' + msg + '</p>')
                    setTimeout(create_abcd, 2000);
            }
            else
                return false;
        }
    });
}

any help would be greatly appreciated!

3
  • 5
    Have you made any attempt at solving this yourself? Should be as simple as setting a variable that is checked before the next iteration runs. Commented May 25, 2012 at 20:20
  • Yes, set a global variable with the button and have the function check the value of the variable on each run Commented May 25, 2012 at 20:21
  • that is something i did not think of! thanks. let me try that and come back here. sorry for being a noob, m new to js but i did try a lot before posting here. Commented May 25, 2012 at 20:23

3 Answers 3

7

Something like:

var needStop = false;

function create_abcd()
{
    var dataString = 'action=create_abcd&type=' + $('#abcd_type').val() + '&count=100';
    $.ajax({
        type: "POST",
        url: "backend.php",
        data: dataString,
        success: function(msg){
            if(needStop) {
                needStop = false;
                return;
            }
            if(msg != "0")
            {
                $("#abcd_output").append('<p>' + msg + '</p>')
                    setTimeout(create_abcd, 2000);
            }
            else
                return false;
        }
    });
}

$('#button').click(function() {
    needStop = true;
});

=)

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

2 Comments

Awesome!! you are a life saver. As for me, one dumb day at a time :)
Update: The second solution worked perfectly but the first one would somehow keep going if i clicked pause during the execution of the function. It works when the function is not running.
2

I think you're trying to solve your problem in a wrong way. You're obviously want to gen notified when some long-running process finishes on the server, so you poll every 2 secs. This will cause a lot of unnecessary requests.

Instead use push mechanism.

Consider using COMET, since you're PHP:

http://www.zeitoun.net/articles/comet_and_php/start

1 Comment

actually no, the process is too huge to be executed in one go on a shared server as it will timeout. but thanks for this info! its a great read! something new to learn about, i can surely use it in my project!!
0

Create a global variable (or even a hidden input on the page).

Create a 'stop' button on the page.

When you click the 'stop' button you just set that input or variable to a special value.

At the top of your create_abcd just check that variable or input before proceeding. If the special value is set, just exit before setting the timeout again.

1 Comment

to add to this: to be 100% sure the function isn't run again after the stop-button is clicked, you should capture the reference to the timeout function and call cleartimeout

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.