title: Creating a session timeout function, which can display timer and redirect to previous page on timeout in angular
body: <p>I want to create a session timeout function for a payment page, where the timer will be displayed in my webpage and after 5 minutes, the payment session will expire and user is redirected to the previous page. I am using angular 4 for my front end, so using <code>href</code> is not entertained, i want a solution involving just <code>javascript</code> and <code>routerLink</code> and <code>typescript</code>. Any suggestions?</p>
  
  <p>I am using this function to show the timer on webpage, its working but not am unable to use routerLink here to redirect to previous page.</p>
  
  <pre><code>paymentSessionTimer() {
  let downloadButton = document.getElementById("paymentSession");
  var counter = 600;
  var newElement = document.createElement("p");
  newElement.innerHTML = "10";
  var id;
  this.redirectFlag = "false";
  
  downloadButton.parentNode.replaceChild(newElement, downloadButton);
  
  id = setInterval(function() {
    counter--;
    if(counter &lt; 0) {
        newElement.parentNode.replaceChild(downloadButton, newElement);
        clearInterval(id);
  
        this.redirectFlag = "true";     
        //this.router.navigate(['../search']);
    } else {
      newElement.innerHTML = counter.toString() + " seconds left.";  
    }
  }, 1000);
  this.router.navigate(['../previous']);
  </code></pre>
  
  <p>}</p>