Showing posts with label JavaScript. Show all posts
Showing posts with label JavaScript. Show all posts

Sunday, March 17, 2013

How to pause execution of javascript for sometime

Today, I was just looking into the ways to pause or wait or delay execution of my javascript statement for some given time. I do some google for this and I got such a really good code snippet - sleep() function from somewhere.

sleep() Function in Javascript

function sleep(milliseconds) { 
        var start = new Date().getTime(); 
        for (var i = 0; i < 1e7; i++) { 
                if ((new Date().getTime() - start) > milliseconds){ break; } 
        } 
}

Usage

//Sleep for 1 second (1000 milliseconds). 
console.log(new Date()); 
console.log('Dude!'); 
sleep(1000); 
console.log(new Date());


Tuesday, December 25, 2012

Multivalue Checkbox - jQuery plugin


Recently, I was looking for multivalue checkbox which gives user to select multiple options. I can do it with radio button but as there isn't required space to give 3 or 4 radio button and it's lable as well as some designing issue I was looking for multivalues checkbox.

Finally, I have made jQuery plugin which convert my html checkbox to multivalue checkbox with it's multiple options icon image. Here I have just hide html checkbox and add one img tag before checkbox which represented the selected option's icon.

You can review full source code on Github repository, and Click Here for DEMO.

Hope this help and don't forget to submit comment or issue.

Friday, December 21, 2012

Numbers in JavaScript

Hello Guys,
Once I wonder that why the I got the 10 value in my javascript variable x in the following example:
var x = 1 + 011;

After, googling some times I got that it was a radix param issue [more].

While parsing into the number javascript assumes the following:

  • If the string begins with "0", the javascript consider the string as octal number and convert it to decimal number. 
  • If the string begins with "0x", the javascript consider the string as hexadecimal number and convert it to decimal number. 
  • If the string begins with any other value, it will consider as decimal number.    
So in above example 011 considered as an octal number and javascript converts it to decimal number 9 and then add 1 to 9 so I got the answer 10.