2 Replies - 1090 Views - Last Post: 05 July 2012 - 12:34 PM

#1 scottyadam   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Firefox crashes with javascript loop for hash bang #!

Posted 03 July 2012 - 03:07 PM

I have created one page that is run by ajax requests. All the links on the page are hash bang so that there is no page refresh but to detect a change in the address bar I thought about setting a background loop. The function is below and don't really know how else it can be done. Problem I am having is that there is so much memory getting used up i presume because firefox just crashes and the other browsers just start freezing not immediately but 5 or so minutes, maybe 10 minutes into script execution. I cannot go the HTML5 route yet because I have to make this work for the older browsers, so, anyone got any other suggestions on how to accomplish this? Very new to this.

function wtpac_ticker(){

  if( location.hash != wtpac_hashurl  ){ // url has changed
    wtpac_hashurl = location.hash // update the stored hash
    wtpac_page()
  }

  // url has not changed, loop back
  setTimeout('wtpac_ticker()',1000)

}



How do companies like facebook deal with these which i presume it is all the time with tickers for chat and news...etc...can I attach or extend a window event?

Is This A Good Question/Topic? 0
  • +

Replies To: Firefox crashes with javascript loop for hash bang #!

#2 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: Firefox crashes with javascript loop for hash bang #!

Posted 03 July 2012 - 03:41 PM

When you're setting up a ticker that repeats constantly for more than a couple of ticks, you really should be using setInterval, rather than setTimeout. The latter is meant for single timeouts, whereas the former is meant to repeatedly call a function.

var wtpac_hashurl;
setInterval(function() {
	if( location.hash != wtpac_hashurl  ){ // url has changed
		wtpac_hashurl = location.hash // update the stored hash
		wtpac_page()
	}
}, 1000);


I don't see how this would cause any sort of performance issues. I've used similar techniques in the past without any trouble.


Also, one other thing. I would advice you to pass the function name to functions like setTimeout, rather than a string to be evaluated.
// Here you are passing a string that is evaluated when the
// time is up.
setTimeout('wtpac_ticker()', 1000);

// But here a function reference is being passed, so it
// can be executed directly.
setTimeout(wtpac_ticker, 1000)


Was This Post Helpful? 1
  • +
  • -

#3 scottyadam   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: Firefox crashes with javascript loop for hash bang #!

Posted 05 July 2012 - 12:34 PM

Thanks man, I will try that. weird, i just upgraded to windows 8 with the newest firefox and don't see any issue and i had it on all night. i was running win7 with new FF so i have no clue what went on...i haven't adjusted any code and with this upgrade things are working. I will do the code change anyhow.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1