Category: Javascript

April 4th, 2013 by willow

Note to self:

It doesn’t work because this is a different context at that point (window), you have a few optons though, store a reference to what you want to deal with, like this:

.ajaxStop(function() { var $this = $(this); setTimeout(function(){ $this.css('visibility','hidden'); }, 100); });

Or use $.proxy() for to set the context in that anonymous function, like this:

.ajaxStop(function() { setTimeout($.proxy(function(){ $(this).css('visibility','hidden'); }, this), 100); });

In the first solution we store a reference to what we want to deal with, in the second we’re actually setting that this is when that function runs…otherwise it’ll be window.

From stackoverflow:

http://stackoverflow.com/questions/3913090/jquery-why-this-doesnt-work-in-a-settimeout-method

Posted in Javascript, Web Development

October 16th, 2012 by willow

I’ve come across it so many times, and each time I forget. So in addition to a tidbit, this is also a note to myself.

How many times has it happened that you’ve been programming, working with Firefox, Chrome or Safari and everything works fine until… you open up  your page in Internet Explorer and you see that awful “unknown runtime error” .

First thing to check if you’re using ajax and trying to set innerHtml is that there are no html syntax errors.

But it can also happen without any ajax, and 9 times out of 10 it turns out the script that’s giving the error needs a delay. You can easily find out by putting an alert() somewhere at the beginning of the script. If it then works, it means you need a delay, which you can obtain using the setTimeout() function.

Here’s a link with information on that function: http://www.w3schools.com/js/js_timing.asp

Posted in Javascript, Web Development