sleep() function in JavaScript

January 6th, 2010

JavaScript doesn't have a handy sleep() or wait() or delay() function to pause the execution for a few seconds, so we can use setTimeout() function to emulate such behaviour. It's not straightforward and may require you to refactor the workflow, but it's delaying the execution and is really simple:

JAVASCRIPT:

  1. setTimeout(start, 1000);
  2.     var i = 0;
  3.    
  4.     function start() {
  5.         console.log('i am here ' + i);
  6.         i++;
  7.         setTimeout(start, 1000);
  8.     }

JTidy and UTF-8 (international characters)

December 15th, 2009

To make JTidy work correctly with UTF-8 strings and process international characters in a proper way, use the following code:

JAVA:

  1. Document doc = Tidy.createEmptyDocument();
  2.         try {
  3.             doc = tidy.parseDOM(new InputStreamReader(IOUtils.toInputStream(html), "UTF-8"), new NullWriter());
  4.         } catch (UnsupportedEncodingException e) {
  5.             log.error(e);
  6.         }

iPhone fring calls work only for WiFi

November 29th, 2009

What a pity! I was really disappointed today - it appears you can't make fring calls (SkypeOut or SIP calls) while you are not using an available WiFi connection on iPhone. After my good old Nokia E71 it looks really weird. We got a new 32 gig iPhone 3GS for my girlfriend and I was thinking about getting the same toy when my current plan is over. But that fact turns me away, sorry apple :)

PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

November 24th, 2009

If you are getting this exception from your Java program using HTTPClient library, that means the site you are trying to connect to has a test/self-signed or invalid certificate. The easiest way to workaround this problem is to grab InsecureSSLProtocolSocketFactory and InsecureTrustManager from HTMLUnit project and before making a request use this code:

JAVA:

  1. final ProtocolSocketFactory factory = new InsecureSSLProtocolSocketFactory();
  2. final Protocol https = new Protocol("https", factory, 443);
  3. Protocol.registerProtocol("https", https);