Free Android CakePHP GMaps Articles by Bali Web Design

July 21, 2010

Sleep or wait process on javascript thread

Filed under: html,javascript — admin @ 10:10 pm

Actually there is no Thread.sleep method on javascript that work on Threading. Moreover there is no true threading in javascript. So everybody that want to deal with threading need to emulate it. In this article i would like to show you some method that emulate sleep method for waiting on a process. Each of this method has each own disadvantage, so you need to choose which is suitable for your case.

First method is sleep using javascript loop. This is first method that come to my mind when emulate sleep method on javascript. We wait for the process by checking the time in the loop.

function wait(millisecond) {
var date = new Date();
var current_date = null;

do { current_date = new Date(); }
while(current_date-date < millisecond)

// do something
wait(1000); // wait 1000 millisecond
// do something


How to handle user closing browser windows or page using javascript

Filed under: html,javascript — admin @ 12:40 am

Sometime we need to handle or do something when user closing a browser windows or page. Maybe we want to ask user if they really want to close the page or it was a mistake click. This procedure all can be done using javascript.

There are two condition or event that you can intercept before it really happen, first when user close the browser windows and second is when browser windows will be closed without any way to abort it.

For first condition we can handle it using window.onbeforeunload event.

function closeHandler() {
return “Are you sure you want to close this page?”;

window.onbeforeunload = closeHandler;