setTimeout doesnt wait like it should

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi,

Im trying to learn how to have a page that after a few seconds it will redirect to another page. I'm having problem with that setTimeout doesnt wait for the milliseconds I want it to but rather after like 2-3 seconds do the redirect. It doesnt matter if I type in 1 millisecond or 30000 millisecond. It happens at the same time,.

My simple code is this, which I have on a new UI page to see how it works..

<?xml version="1.0" encoding="utf-8" ?>

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">

My text

<script>      

     

      setTimeout(redirectNavPage(),10000);

     

      function redirectNavPage()

      {

              var URL = "/navpage.do";

              top.window.location = URL;

      }

</script>      

</j:jelly>

I put 10000 milliseconds just so I should notice that it's working.

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Well,


I found it myself 😃



I should have setTimeout(redirectNavPage,10000) instead of setTimeout(redirectNavPage(),10000);
if I have the () on the function it will hit instantly... Just if anyone else beside me runs into this 😎


View solution in original post

3 REPLIES 3

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Well,


I found it myself 😃



I should have setTimeout(redirectNavPage,10000) instead of setTimeout(redirectNavPage(),10000);
if I have the () on the function it will hit instantly... Just if anyone else beside me runs into this 😎


Too bad I can't pass variables now, but at least it explains why it wasn't waiting anymore


Sure you can:
setTimeout(function(){ redirectNavPage(param1, param2, param3); }, 10000);