1:
, , , , focus(), , . jQuery 1.5, HTML, , jquery.data().
2:
, , , count interval , . - , .data() , , , .
PHP:
function displayMult ($i)
{
echo '<table class="basic">';
for($k=0; $k < $i; ++$k)
{
$user_response[$k] = 'user_response' . $k ;
echo '<tr>';
echo '<td>' . number_format($_SESSION['mult_one'][$k]) . '</td>';
echo '<td>'. ' x ' . '</td>';
echo '<td>' . number_format($_SESSION['mult_two'][$k]) . '</td>';
echo '<td>' . ' = <input data-timer="#timer_' . $k . '" type="text" class="field" name="user_response' . $k . '"' . 'id="u_r' . $k . '" />' . '</td>';
echo '<td id="timer_' . $k . '">test</td>';
echo '</tr>';
}
echo '</table>';
}
JS:
$(document).ready(function() {
$('.field').focus(function() {
var timer_id = $(this).data("timer");
stopwatch("#" + timer_id);
});
$('.field').blur(function() {
var timer_id = $(this).data("timer");
stopwatch("#" + timer_id);
});
});
function stopwatch(container_id) {
var $container = $(container_id);
var time_now = new Date();
var time_started = $container.data("time");
if (time_started) {
time_taken = time_now - time_started;
$container.html(time_taken / 1000 + ' seconds');
}
else {
$container.data("time", time_now);
}
}