118551
Goto Top

JavaScript Button Problem

hallo ich wollte für ein Script testen ob ein Button immer noch Gedrückt ist

nur auf google finde ich zu diesen Thema nix! face-sad

für die bessere Vorstellung was gemeint ist mache ich mal ein Beispiel Script (Leider nur als bild)


HTML5
<DOCTYPE html>
<html>
<head>
</head>
<body>
<span>setz ein häckchen so lange du willst</span><input type="checkbox" id="B1" onclick="testfunction()">  
<input type="text" id="Länge" value="0ms" readonly>  
<script>
var button = document.getElementById("B1");  
var TA = document.getElementById("Länge");  
var i=0;

function testfunction(){

if(button.checked){

setTimeout(function() {

i++
TA.value = i + "ms";  
testfunction();

},1)

}else{

i=0;

}

}
</script>
</body>
</html>

2 Probleme

1. Ich hätte gerne ein Button!
2. Der Zähler zählt in irgendwas aber nicht in Sekunden oder Millisekunden

Danke im voraus für die Hilfe!

Content-Key: 285952

Url: https://administrator.de/contentid/285952

Ausgedruckt am: 28.03.2024 um 13:03 Uhr

Mitglied: colinardo
Lösung colinardo 18.10.2015 aktualisiert um 17:43:50 Uhr
Goto Top
Hallo DomiZone1,
guckst du hier:
https://jsfiddle.net/hhvhm73r/
<!doctype html>
<html>
<head>
<meta charset="utf-8">  
<title>Demo</title>
</head>
<script type="text/javascript">  
	var starttime;
	var textbox;
	var total;
	var interval;
	function count(){
		total = (new Date()).getTime() - starttime;
		textbox.value = total + "ms";  
	}	
	function mousedown(){
		starttime = (new Date()).getTime();
		total = 0;
		textbox = document.getElementById('dauer');  
		interval = window.setInterval("count()",1);  
	}
	function mouseup(){
		window.clearInterval(interval);
	}
</script>
<body>
<input type="button" value="Drück mich so lange du willst" onMouseDown="mousedown()" onMouseUp="mouseup()" />  
<input type="text" id="dauer" value="0ms" readonly>  
</body>
</html>
Grüße Uwe
Mitglied: 118551
118551 18.10.2015 aktualisiert um 17:31:08 Uhr
Goto Top
Danke Für deine Hilfe

LG DomiZone1