Q:

clearInterval from outside a function

I want to make a timing function a can display countdown. I made it this way. and when the time is over it will clearInterval(timing)
BUT I wanted to be able to interrupt the function within the countdown and clearInterval(timing).
    var timing = function(x,callback) {
        var seconds = x;
        var second = 1;
        var timing;

        $("#timing h1").text(seconds);
        $("#timing").show();
        timing = setInterval(function() {
            $("#timing h1").text(seconds - second);
            if (second >= (seconds - second) ) {
                clearInterval(timing);
                $("#timing").hide();
                callback();
            }
            seconds--;
        }, 1000);
    }

A:

You need to make the _timing (renamed) variable global. So, the following code should work:
var timing = function(x, callback) {
    var seconds = x;
    var second = 1;
    $("#timing h1").text(seconds);
    $("#timing").show();
    _timing = setInterval(function() {
        $("#timing h1").text(seconds - second);
        if (second >= (seconds - second)) {
            clearInterval(_timing);
            $("#timing").hide();
            callback();
        }
        seconds--;
    }, 1000);
}

