我14岁的儿子正在研究一个科学项目,研究反应时间和年龄。他正在设置一个小型的网络应用程序来测试人们 - 当一个页面被加载时,一个计时器启动并且出现一个STOP按钮的延迟(本例为4秒)。当他们点击停止按钮时,计时器停止。 到目前为止,他完成了所有这些编码工作。他正在使用他发现的一段JavaScript,并根据自己的需要对其进行了修改。
他的问题 - 如何将停止的时间传递给一个变量,然后将其传递给另一个页面。如果变量是静态的,即“你好”,他能够成功地做到这一点。 函数stop()有什么问题;在这个例子中?他目前得到[object HTMLSpanElement]将JavaScript值变为变量
var clsStopwatch = function() {
// Private vars
var startAt = 0; // Time of last start/resume. (0 if not running)
var lapTime = 0; // Time on the clock when last stopped in milliseconds
var now = function() {
return (new Date()).getTime();
};
// Public methods
// Start or resume
this.start = function() {
startAt = startAt ? startAt : now();
};
// Stop or pause
this.stop = function() {
// If running, update elapsed time otherwise keep it
lapTime = startAt ? lapTime + now() - startAt : lapTime;
startAt = 0; // Paused
};
// Reset
this.reset = function() {
lapTime = startAt = 0;
};
// Duration
this.time = function() {
return lapTime + (startAt ? now() - startAt : 0);
};
};
var x = new clsStopwatch();
var $time;
var clocktimer;
function pad(num, size) {
var s = "0000" + num;
return s.substr(s.length - size);
}
function formatTime(time) {
var h = m = s = ms = 0;
var newTime = '';
h = Math.floor(time/(60 * 60 * 1000));
time = time % (60 * 60 * 1000);
m = Math.floor(time/(60 * 1000));
time = time % (60 * 1000);
s = Math.floor(time/1000);
ms = time % 1000;
newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2) + ':' + pad(ms, 3);
return newTime;
}
function update() {
$time.innerHTML = formatTime(x.time());
}
function start() {
$time = document.getElementById('time');
update();
clocktimer = setInterval("update()", 1);
x.start();
$(document).ready(function() { $('#mybutton').delay(4000).fadeIn(0);});
}
function stop() {
x.stop();
//var varTime = "Hello";
var varTime = document.getElementById('time');
window.location.href = "somephpfile.php?etime=" + varTime;
}
试试看:'VAR varTime =的document.getElementById( '时间')的innerText;' – Rayon
当前的varTime会返回一个对象(我相信这是一个DIV或P)。你必须使用.innerText来获取对象内的文本值:)。希望这可以帮助 –