How to set a timeout on a portal page

servicegirl
Kilo Contributor

I am trying to figure out how to set a timeout on a portal page that redirects back to the portal homepage after 10 seconds. How can I go about this?

Thanks

1 ACCEPTED SOLUTION

You have several angular directives that are wrappers that will do what you want...



In your Client Controller field in the widget, just make sure you pass in the $window and $timeout parameters so you have access to them...



function ($scope, $window, $timeout) {


  var c = this;



//


// ... put your code in here for your widget


//



  $timeout(function() {


      $window.location.href="the url to redirect to";


  }, 10000, false);


}




However, this simple code will redirect at 10 seconds regardless of what the user is doing so if they have half of their stuff filled out it will redirect while they may be in mid key-stroke so you probably want to add some intelligence into your widget and how it interacts before you pull the trigger on the rewrite.


View solution in original post

4 REPLIES 4

richard-service
ServiceNow Employee
ServiceNow Employee

Just like any other web page you can apply a simple timer via JS.



window.onload = function() {


      setTimeout(function(){ window.location.href = "https://myinstance/mysite"; }, 10000);


}



Can I ask why you are looking to do this?


I have an application that has users generate tickets, it sits on an iPad stand. If a page on the app has been sitting too long I want it to redirect to the homepage so when the next user walks by to use it they don't get confused as to how to create their ticket. I am familiar with how to implement a timer in JS, the problem is, is where to add this function so it works on the entire page. I tried putting it on a widget but that didn't work. Do you know where I would code this?


You have several angular directives that are wrappers that will do what you want...



In your Client Controller field in the widget, just make sure you pass in the $window and $timeout parameters so you have access to them...



function ($scope, $window, $timeout) {


  var c = this;



//


// ... put your code in here for your widget


//



  $timeout(function() {


      $window.location.href="the url to redirect to";


  }, 10000, false);


}




However, this simple code will redirect at 10 seconds regardless of what the user is doing so if they have half of their stuff filled out it will redirect while they may be in mid key-stroke so you probably want to add some intelligence into your widget and how it interacts before you pull the trigger on the rewrite.


Katie A
Mega Guru

If you want to display a countdown timer of the number of seconds remaining before the redirect, you can use the $interval method as well.

// Client Script
function ($timeout, $window, $interval) {
  /* widget controller */
  var c = this;

  // Set redirect seconds
  c.redirect_seconds = 30;

  // Convert redirect seconds to milliseconds
  c.redirect_ms = c.redirect_seconds * 1000;

  // Initializes client-side session interval timer and decrements countdown value
  c.sessionIntervalTimer = $interval(function () {
    // Decrement session countdown value
    c.redirect_seconds--;

    // Execute session countdown timer in 1000ms (1 second) intervals
  }, 1000);

  // Redirect
  $timeout(function () {
    $window.location.href = "?id=example_homepage";
  }, c.redirect_ms, false);

}
<!-- HTML -->
<div>
<p>The landing page will be displayed automatically in {{c.redirect_seconds}} seconds</p>
</div>