2016-06-16 5 views
2

Ich habe ein Problem mit einer Inaktivitätsprüfung für meine Website. Ich habe ein Codebeispiel unter StackOverflow gefunden, um eine einfache Inaktivitätsüberprüfung durchzuführen. Und das funktioniertInaktivitätsprüfung in Typoskript

var inactivityTime = function() { 
 
    var t; 
 
    window.onload = resetTimer; 
 
    document.onmousemove = resetTimer; 
 
    document.onkeypress = resetTimer; 
 

 
    function logout() { 
 
     location.href = 'logout.aspx' 
 
    } 
 

 
    function resetTimer() { 
 
     clearTimeout(t); 
 
\t \t t = setTimeout(logout, 1000 * 60 * 1); 
 
    } 
 

 
};

Da ich Typoskript mache ich den Code in eine Klasse neu geschrieben. Die Inaktivitätsprüfung funktioniert. es navigiert zu einer anderen Seite. Das Problem, das ich habe, ist, dass es auch zur anderen Seite navigiert, wenn es Aktivität gibt. Ich debuggte den Code und der Cleartimeout-Code wird aufgerufen, aber irgendwie wird es immer noch ausgeführt. Weiß jemand, was ich falsch mache? ist dies die Klasse I in Typoskript gemacht (v1.7)

class Inactivity { 
 

 
\t private timerHandle: number = null; 
 

 
\t constructor() { 
 
\t } 
 

 
\t startChecking() { 
 
\t \t this.resetTimer(); 
 
\t \t window.onload = this.resetTimer; 
 
\t \t document.onmousemove = this.resetTimer; 
 
\t \t document.onkeypress = this.resetTimer; 
 
\t } 
 

 
\t stopChecking() { 
 
\t \t if (this.timerHandle) { 
 
\t \t \t clearTimeout(this.timerHandle); 
 
\t \t \t this.timerHandle = null; 
 
\t \t } 
 
\t } 
 

 
\t private resetTimer() { 
 
\t \t if (this.timerHandle) { 
 
\t \t \t clearTimeout(this.timerHandle); 
 
\t \t \t this.timerHandle = null; 
 
\t \t } 
 
\t \t this.timerHandle = setTimeout(this.logout, 1000 * 60 * 1); 
 
\t } 
 

 
\t private logout() { 
 
\t \t location.href = 'logout.aspx'; 
 
\t } 
 

 
}

und das ist, wo ich es nennen:

module Home { 
 
    \t var inactivity: Inactivity; 
 

 

 
    export function init() { 
 
     //inactivityTime(); //javascript code that works 
 
     inactivity = new Inactivity(); 
 
     inactivity.startChecking(); 
 
    } 
 
}

+0

Wo wird die Funktion init() aufgerufen? Es kann mehrere Male aufgerufen werden und Sie können mit mehreren verschiedenen Instanzen von Inaktivität enden. –

+0

wird von der HTML-Seite aufgerufen

Antwort

3

Ich denke, Das Problem liegt wahrscheinlich im Kontext. Versuchen Sie, den richtigen Wert für this zu binden:

class Inactivity { 

    private timerHandle: number = null; 

    constructor() { 
    } 

    startChecking() { 
     this.resetTimer(); 

     // Bind the methods to the proper context 
     window.onload = this.resetTimer.bind(this); 
     document.onmousemove = this.resetTimer.bind(this); 
     document.onkeypress = this.resetTimer.bind(this); 
    } 

    stopChecking() { 
     if (this.timerHandle) { 
      clearTimeout(this.timerHandle); 
      this.timerHandle = null; 
     } 
    } 

    private resetTimer() { 
     if (this.timerHandle) { 
      clearTimeout(this.timerHandle); 
      this.timerHandle = null; 
     } 
     this.timerHandle = setTimeout(this.logout, 1000 * 60 * 1); 
    } 

    private logout() { 
     location.href = 'logout.aspx'; 
    } 

} 
+0

Dies war in der Tat das Problem. Vielen Dank. –