Saturday, February 04, 2006

 

Marquee - Beta

This is only an announce, here will be the content of the article, the code is on:
http://www.danguer.com/articles/diaries/javascript/marquee/
And simulates a marquee similar to the one on blogger, it has a bug on safari, that's why it's beta.
Comments about the code working/non working are welcome.

Monday, January 30, 2006

 

Double Click

This is an easy way to implement double click in Javascript:
Code

var last = 0;
var delay = 400; /*the number of miliseconds allowed
between a couple of clicks to be
taked as double click */

function doubleClick()
{
time = new Date();
lap = time.getTime();
click = document.getElementById("click");

if (lap - last < delay) //if it's less than 0.4 seconds
{
/* part to fire the event, you can delete the following line */
click.innerHTML = "Double Click ["+lap+" - "+last+"]";
last = 0;
}
else
{
last = lap;
click.innerHTML = "";
}
}


As you can see, the code is simple, the part of: if (lap - last < delay) is where the event is fired, if has been double click between a certain delay, in this case 400 miliseconds (0.4 seconds).
In this case I'm showing an alert in a div when the double click happens, when there is no double click, I only clean the div (else part).

All the page is:
Code

<html>
<head>
<script language="JavaScript">
var last = 0;
var delay = 400; //the number of miliseconds allowed
//between a couple of clicks to be
// taked as double click

function doubleClick()
{
time = new Date();
lap = time.getTime();
click = document.getElementById("click");

if (lap - last < delay) //if it's less than 0.4 seconds
{
click.innerHTML = "Double Click ["+lap+" - "+last+"]";
last = 0;
}
else
{
last = lap;
click.innerHTML = "";
}
}
</script>
</head>
</html>

<body>
<a href="javascript:doubleClick();">Double click on this link</a>
<div id="click"></div>
</body>
</html>


Other Ways to implement double click


The most "standard" way to implement double click is simple using ondblclick event (<a href="void(0);" ondblclick="doubleClick();">Double Click</a>, which seems to work without problems in IE and Netscape, but of course you can always rely on a self implementation and look as a hacker =)

Example


Test Double Click

This page is powered by Blogger. Isn't yours?