Good day. To the point. When you run the setTime function, we pass input to it, where the time is displayed. question. when re-called through a timer, obj is not defined. and even if the inputs through the var to define does not work. only in this way. here code
var id_inter; var go = true; function setTime(obj) { data = new Date() hours = data.getHours(); mins = data.getMinutes(); secs = data.getSeconds(); if (hours < 10) { hours = "0" + hours; } if (mins < 10) { mins = "0" + mins; } if (secs < 10) { secs = "0" + secs; } time = hours + ":" + mins + ":" + secs; obj.value = " " + time; id_inter = setTimeout('setTime(obj)', 1000); } function stopTime(obj) { clearTimeout(id_inter); obj.value = ""; } function times(input_form) { if (go) { setTime(input_form); go = false; document.data.start.value = "Стоп"; } else { stopTime(input_form); go = true; document.data.start.value = "Старт"; } } If you write instead
obj.value = " " + time; id_inter = setTimeout('setTime(obj)', 1000); So:
inputs = obj; id_inter = setTimeout('setTime(inputs)', 1000); then it works.
Why?
https://jsfiddle.net/jL3fhLd3/4/
setTimeout(func / code, delay[, arg1, arg2...])would write like this:id_inter = setTimeout(setTime, 1000, obj);..... so it works. But the behavior is interesting .. Wait until someone answers. - Alexey Shimansky