AS3 - Using ExernalInterface.call To Reset JS Timer

114 views Asked by At

I'm trying to build a 'dead mans switch' into a flash app. When the app crashes, or slows down significantly, I would like the webpage to refresh. From what I've read and seen here, it's possible to use ExernalInterface to call a resetTimer JS function.

Heres my AS3..

//External Timer and Handler
var externaltimer: Timer = new Timer(1000);
externaltimer.addEventListener(TimerEvent.TIMER, callTimerJS);
externaltimer.start();

function callTimerJS(event : Event):void{
  ExternalInterface.call("window.clearTimeout");
  ExternalInterface.call("timeoutHandle");

}

Heres my JS in my HTML page...

    var timeoutHandle = window.setTimeout(function() {
    window.location.reload(1);
}, 15000);

window.clearTimeout(timeoutHandle);

timeoutHandle = window.setTimeout(function() {
    window.location.reload(1);
}, 15000);

I'm not sure how to test this to verify it's working. I do know when I build, the webpage refreshes every 15 seconds. I'm not able to get flash to reset the timer.

1

There are 1 answers

6
Andre Lehnert On

Use normal function-declarations and be sure that is not a js-core-function-name. You can call only functions.

AS

//External Timer and Handler
var externaltimer: Timer = new Timer(1000);
externaltimer.addEventListener(TimerEvent.TIMER, callTimerJS);
externaltimer.start();

function callTimerJS(event : Event):void{
    if (ExternalInterface.available) {
        ExternalInterface.call("clearTimeoutfromExtern");
    } 
}

JS

var timeoutHandle;
function clearTimeoutfromExtern(){
     clearTimeout(timeoutHandle);
     timeoutHandle = setTimeout(refresh, 15000);
}
function refresh(){
    window.location.reload(1);
}

I just wrote it here in the editor... Greetings.