2009/8/23 solow <solow.wes...@gmail.com>:
> function joinRoomFunction(roomId){
>        $("#tables").load("pages/pages.php", {r: roomId}, function()
> {
>                $("#myTable").tablesorter({widgets: ['zebra']});
>                alert('loaded');
>                 $('button#stopINTERVAAL').click(function()
> {intvarM=clearInterval(intvarM);});
>        });
>        }
>    function joinRoom(roomId){
>        var intvarM=window.setInterval("joinRoomFunction("+ roomId
> +")",5000);
>    }

A few points: you are assigning the interval identifier to intvarM
inside a function using "var intvarM = setInterval...". The use of the
"var" keyword will make that variable local to that function. You need
to declare the variable in some outer scope, so that it is also
visible in the click handler you assign to the button.

Also, clearInterval does not return a value (or, to be absolutely
precise, the return value of clearInterval is "undefined"), so there
is probably no point assigning it to anything.

Finally, by assigning a string to setInterval you are basically
performing an "eval", which is very inefficient. Creating a function
reference using a closure is the preferred approach.

Try this:

var intvarM; // as this is declared out here, it will be visible to
all the functions below

function joinRoomFunction(roomId){
    $("#tables").load("pages/pages.php", {r: roomId}, function() {
        $("#myTable").tablesorter({widgets: ['zebra']});
        alert('loaded');
        $('button#stopINTERVAAL').click(function() {
            window.clearInterval(intvarM); // this method returns undefined
        });
     });
}

function joinRoom(roomId){
    intvarM = window.setInterval((function(roomId) {
        return function() { // return a function to be used by the timer
            joinRoomFunction(roomId);
        }
    })(roomId), 5000);
}

Regards,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/

Reply via email to