Показать сообщение отдельно
  #1 (permalink)  
Старый 03.10.2016, 20:34
Интересующийся
Отправить личное сообщение для svinin_1989 Посмотреть профиль Найти все сообщения от svinin_1989
 
Регистрация: 03.10.2016
Сообщений: 11

setTimeout не хочет исполнять функцию
Помогите люди добрые. Делаю калькулятор, пытаюсь избавится от кнопки =. Хочу чтоб автоматом считал результат в поле результат.
Кнопочка равно инициализирует
function compute(form) {
	form.result.value = eval(form.display.value)
}


Все работает. Пытаюсь запустить
function compute(form) {
	form.result.value = eval(form.display.value)
}

setTimeout(compute(form), 1000);



Не срабатывает.

Вот страничка вся
<!DOCTYPE HTML>
<html>
<head> </head>
<body>
<font color="RED" size="18"> <B>КАЛЬКУЛЯТОР</B></font> <br/>
<form name="sci-calc">
<table table border="1"  >
<tbody> 
<tr>
<td align="center" style="background-color: red"  ><input type="button" value="1" onclick="addChar(this.form.display, '1')"></td>
<td align="center"  ><input type="button" value="2" onclick="addChar(this.form.display, '2')"></td>
<td align="center"  ><input type="button" value="3" onclick="addChar(this.form.display, '3')"></td>
<td align="center"  ><input type="button" value="4" onclick="addChar(this.form.display, '4')"></td>
<td align="center"  ><input type="button" value="5" onclick="addChar(this.form.display, '5')"></td>
<td align="center"  ><input type="button" value="6" onclick="addChar(this.form.display, '6')"></td>
<td align="center"><input type="button" value="7" onclick="addChar(this.form.display, '7')"></td>
<td align="center"><input type="button" value="8" onclick="addChar(this.form.display, '8')"></td>
<td align="center"><input type="button" value="9" onclick="addChar(this.form.display, '9')"></td>
<td align="center"><input type="button" value="0" onclick="addChar(this.form.display, '0')"></td>
<td align="center"><input type="button" value="." onclick="addChar(this.form.display, '.')"></td>
</tr>
 


<tr>
<td colspan="6" align="center"><input name="display" value="0" size="55" maxlength="55"></td>
<td align="center"><input type="button" value="X" onclick="this.form.display.value = 0 "></td>
<td align="center"  ><input type="button" value="<" onclick="deleteChar(this.form.display)"></td>

<td align="center"><input type="button" value="=" name="enter" onclick="if (checkNum(this.form.display.value)) { compute(this.form) }"></td>

 <td colspan="3" align="center"><input name="result"   size="20" maxlength="20"></td>
<td align="center"><input type="button" value="X" onclick="this.form.result.value = 0 "></td>
<td align="center"  ><input type="button" value="<" onclick="deleteChar(this.form.result)"></td>
<td align="center"><input type="button" value="№" onclick="if (checkNum(this.form.display.value)) { sqrt(this.form) }"></td>
<td align="center"><input type="button" value="^2" onclick="if (checkNum(this.form.display.value)) { square(this.form) }"></td>
<td align="center"><input type="button" value="exp" onclick="if (checkNum(this.form.display.value)) { exp(this.form) }"></td>

</tr>


<tr>
 
<td align="center"><input type="button" value="*" onclick="addChar(this.form.display, '*')"></td>
<td align="center"><input type="button" value="-" onclick="addChar(this.form.display, '-')"></td>
  
<td align="center"><input type="button" value="/" onclick="addChar(this.form.display, '/')"></td>
<td align="center"><input type="button" value="+/-" onclick="changeSign(this.form.display)"></td>
<td align="center"><input type="button" value="+" onclick="addChar(this.form.display, '+')"></td>

 
<td align="center"><input type="button" value="(" onclick="addChar(this.form.display, '(')"></td>
<td align="center"><input type="button" value=")" onclick="addChar(this.form.display, ')')"></td>

<td align="center"><input type="button" value="ln" onclick="if (checkNum(this.form.display.value)) { ln(this.form) }"></td>
 
<td align="center"><input type="button" value="cos" onclick="if (checkNum(this.form.display.value)) { cos(this.form) }"></td>
<td align="center"><input type="button" value="sin" onclick="if (checkNum(this.form.display.value)) { sin(this.form) }"></td>
<td align="center"><input type="button" value="tan" onclick="if (checkNum(this.form.display.value)) { tan(this.form) }"></td>
 </tr>

</tbody></table>
</form>


<script>

function addChar(input, character) {
	if(input.value == null || input.value == "0")
		input.value = character
	else
		input.value += character
}

function cos(form) {
	form.result.value = Math.cos(form.display.value);
}

function sin(form) {
	form.result.value = Math.sin(form.display.value);
}

function tan(form) {
	form.result.value = Math.tan(form.display.value);
}

function sqrt(form) {
	form.result.value = Math.sqrt(form.display.value);
}

function ln(form) {
	form.result.value = Math.log(form.display.value);
}

function exp(form) {
	form.result.value = Math.exp(form.display.value);
}

function deleteChar(input) {
	input.value = input.value.substring(0, input.value.length - 1)
}

function changeSign(input) {
	if(input.value.substring(0, 1) == "-")
		input.value = input.value.substring(1, input.value.length)
	else
		input.value = "-" + input.value
}



 




setTimeout(compute(form), 1000);

function compute(form) {
	form.result.value = eval(form.display.value)
}

function square(form) {
	form.result.value = eval(form.result.value) * eval(form.result.value)
}

function checkNum(str) {
	for (var i = 0; i < str.length; i++) {
		var ch = str.substring(i, i+1)
		if (ch < "0" || ch > "9") {
			if (ch != "/" && ch != "*" && ch != "+" && ch != "-" && ch != "."
				&& ch != "(" && ch!= ")") {
				alert("invalid entry!")
				return false
				}
			}
		}
		return true
}

    </script>


</body>
</html>

Последний раз редактировалось svinin_1989, 03.10.2016 в 20:54.
Ответить с цитированием