我正在尝试在服务器上为我正在创建的游戏设置一个计时器,但我不断收到“在非对象上调用成员函数 stop()”错误。
为了开始时间,我进行了以下 ajax 调用
$.post('game.php', {
action: 'start'
}, function(res) {
},'json');
游戏结束后,我尝试通过进行以下 ajax 调用来停止计时器
$.post('game.php', {
action: 'stop'
}, function(res) {
},'json');
game.php 代码是
$action = $_POST['action'];
switch($action) {
case 'start':
$gameTime = new timer();
$gameTime->start();
break;
case 'stop':
$gameTime->stop();
break;
}
class Timer {
var $classname = "Timer";
var $start = 0;
var $stop = 0;
var $elapsed = 0;
# Constructor
function Timer( $start = true ) {
if ( $start )
$this->start();
}
# Start counting time
function start() {
$this->start = $this->_gettime();
}
# Stop counting time
function stop() {
$this->stop = $this->_gettime();
$this->elapsed = $this->_compute();
}
# Get Elapsed Time
function elapsed() {
if ( !$elapsed )
$this->stop();
return $this->elapsed;
}
# Get Elapsed Time
function reset() {
$this->start = 0;
$this->stop = 0;
$this->elapsed = 0;
}
#### PRIVATE METHODS ####
# Get Current Time
function _gettime() {
$mtime = microtime();
$mtime = explode( " ", $mtime );
return $mtime[1] + $mtime[0];
}
# Compute elapsed time
function _compute() {
return $this->stop - $this->start;
}
}
当我调用停止计时器时,我收到错误消息。我试图找出问题所在,想知道是不是因为我在进行 ajax 调用?
有谁知道让这个工作的方法?