-1

In my JQuery, I have a some code that goes to database after each time interval mentioned in my Web.config...I have a button as well, on clicking the button should stop the current SetInterval to 10 seconds... I am using below mentioned code..

window.setTimeout(MyFunction, 10000);

MyFunction is the callback

function MyFunction() {
    window.setTimeout(MyFunction, 10000);
}

Controller Actions

public JsonResult GetUpdates()
{
}

Confusion is It still going to the database again.

4

2 回答 2

1

If you want to pause the function execution you could have a condition inside MyFunction which is checking whether a button is clicked and not register another setTimeout callback:

function MyFunction() {
    if (window.pauseExecution) {
        return;
    }

    // do the processing here ...

    // schedule another call in 10 seconds
    window.setTimeout(MyFunction, 10000);
}

and when you click on the button simply set the window.pauseExecution global variable to true. And if you want to resume, set it to false.

于 2013-03-18T12:36:19.657 回答
0
var Interval = window.setTimeout(MyFunction, 10000);

ClearInterval(Interval);

    setTimeout(function () {
        //Code
    }, 10000);
于 2013-03-19T11:07:33.487 回答