17 Feb 2012

javascript: too much recursion

Say your website wants to let a user, who happens to be browsing in another tab, know it needs attention. Your only option then seems to be changing the page title to get attention. Facebook does this for new messages in a chat. I thought I'd give it a try.

So I did this:

<script>
function set_title(Msg, T) {
  if (T ==1) {
      document.title("default title");
      setTimeout(set_title(Msg, 1), 1000);
  } else {
     document.title(Msg);
     setTimeout(set_title(Msg, 0), 1000);
  }
}
</script>

But then Firefox said "error: too much recursion"
I tried many different forms of recursion, with and without parameters and globals.

In the end I settled for:

<script>
 document.title(Msg);
setTimeout(document.title("default title"), 1000);
setTimeout(document.title(Msg), 2000);
setTimeout(document.title("default title"), 3000);
setTimeout(document.title(Msg), 4000);
setTimeout(document.title("default title"), 5000);
setTimeout(document.title(Msg), 6000);
</script>

Anyone for a better solution?


No comments:

Post a Comment