JavaScript Function Issue (dates) -
i'm having issues javascript code. need make code following:
- click image twice within 3 seconds make image disappear
- click once reappear
i've got close working...i'm having issues date. don't know how keep date first click. code right creating new start date each click don't want.
code far:
var imgnext = -1; var start = new date ( ); function disappear () { var end = new date (); imgnext++; if (imgnext == 2) { document.getelementbyid("mypicture").style.visibility="visible"; imgnext = -1; } if (imgnext == 1 && (end-start <3000)) { document.getelementbyid("mypicture").style.visibility="hidden"; } start = new date ();
}
in code image changes if clicks on 3 seconds apart because i'm creating new start date every time function triggered. how resolve this?
cheers.
here answer, working example: http://jsfiddle.net/ys5bs/2/
document.getelementbyid("disappear").onclick = function() { var lastclick = this.attributes["click-time"], click = new date().gettime(), timeout = parseint(this.attributes["data-timeout"].value); if(lastclick && (click - lastclick) < timeout && this.style.opacity == 1) { this.style.opacity = 0; this.attributes["click-time"].value = null; return; } else { this.attributes["click-time"] = click; } if(this.style.opacity == 0) { this.style.opacity = 1; this.attributes["click-time"] = null; return; } };
basically assigning onclick, , getting click times, if within set timeout (milliseconds) hides, otherwise, shows again, per spec
Comments
Post a Comment