Я пишу приложение игры, которое запускается в веб-браузере. До сих пор я получил почти все, что написано на PHP и HTML/CSS. Моя текущая цель - вызвать функцию PHP, когда выполняется определенное событие Javascript. Точнее, когда (JS) currentTime достигает 0 в demo.js, я хочу установить php bool в true или напрямую вызвать функцию php из класса.game.phpКак вызвать функцию PHP из события сценария .js?
Это часть моего index.php где я реализую элемент таймера.
Здравствуйте
Я пишу пробегов Wich игра Aplication в веб-браузере. До сих пор я получил почти все, что написано на PHP и HTML/CSS. Моя текущая цель - запустить функцию PHP, когда выполняется определенное событие JS. Точнее, когда (JS) currentTime достигает 0 в demo.js, я хочу установить php bool в true или напрямую вызвать функцию php из class.game.php. Ниже я покажу вам части кода.
некоторые включает в себя:
index.php
<script type="text/javascript" src="inc/jquery.min1.js"></script>
<script type="text/javascript" src="inc/jquery.timer.js"></script>
<script type="text/javascript" src="inc/demo.js"></script>
Это часть моего файла index.php, где я реализую элемент таймера.
index.php
<div id="countdown" style="float: right; margin-top: 60px; margin-right: 30px">01:00:00 </div>
<form id="example2form" style="float: right; margin-top: 25px;">
<input type='button' value='Play/Pause' onclick='Example2.Timer.toggle();' />
<input type='button' value='Stop/Reset' onclick='Example2.resetCountdown();' />
<input type='text' name='startTime' value='300' style='width:30px;' />
</form>
это часть demo.js
demo.js
/**
* countdown function
*/
var Example2 = new (function() {
var $countdown,
$form, // Form used to change the countdown time
incrementTime = 70,
currentTime = 1000,
updateTimer = function() {
$countdown.html(formatTime(currentTime));
if (currentTime == 0) {
Example2.Timer.stop();
timerComplete();
Example2.resetCountdown();
return;
}
currentTime -= incrementTime/10;
if (currentTime < 0) currentTime = 0;
},
timerComplete = function() {
alert('Example 2: Countdown timer complete!');
},
init = function() {
$countdown = $('#countdown');
Example2.Timer = $.timer(updateTimer, incrementTime, true);
$form = $('#example2form');
$form.bind('submit', function() {
Example2.resetCountdown();
return false;
});
};
this.resetCountdown = function() {
var newTime = parseInt($form.find('input[type=text]').val()) * 100;
if (newTime > 0) {currentTime = newTime;}
this.Timer.stop().once();
};
$(init);
});
jquery.timer.js
;(function($) {
$.timer = function(func, time, autostart) {
this.set = function(func, time, autostart) {
this.init = true;
if(typeof func == 'object') {
var paramList = ['autostart', 'time'];
for(var arg in paramList) {if(func[paramList[arg]] != undefined) {eval(paramList[arg] + " = func[paramList[arg]]");}};
func = func.action;
}
if(typeof func == 'function') {this.action = func;}
if(!isNaN(time)) {this.intervalTime = time;}
if(autostart && !this.isActive) {
this.isActive = true;
this.setTimer();
}
return this;
};
this.once = function(time) {
var timer = this;
if(isNaN(time)) {time = 0;}
window.setTimeout(function() {timer.action();}, time);
return this;
};
this.play = function(reset) {
if(!this.isActive) {
if(reset) {this.setTimer();}
else {this.setTimer(this.remaining);}
this.isActive = true;
}
return this;
};
this.pause = function() {
if(this.isActive) {
this.isActive = false;
this.remaining -= new Date() - this.last;
this.clearTimer();
}
return this;
};
this.stop = function() {
this.isActive = false;
this.remaining = this.intervalTime;
this.clearTimer();
return this;
};
this.toggle = function(reset) {
if(this.isActive) {this.pause();}
else if(reset) {this.play(true);}
else {this.play();}
return this;
};
this.reset = function() {
this.isActive = false;
this.play(true);
return this;
};
this.clearTimer = function() {
window.clearTimeout(this.timeoutObject);
};
this.setTimer = function(time) {
var timer = this;
if(typeof this.action != 'function') {return;}
if(isNaN(time)) {time = this.intervalTime;}
this.remaining = time;
this.last = new Date();
this.clearTimer();
this.timeoutObject = window.setTimeout(function() {timer.go();}, time);
};
this.go = function() {
if(this.isActive) {
this.action();
this.setTimer();
}
};
if(this.init) {
return new $.timer(func, time, autostart);
} else {
this.set(func, time, autostart);
return this;
}
};
})(jQuery);
часть class.game.php
/**
* Purpose: end the game
* Preconditions: turns on the game over flag
* Postconditions: game over flag is true
**/
function end()
{
$this->over = true;
}
/**
* Purpose: return bool to indiciate whether or not the game is over
* Preconditions: none
* Postconditions: returns true or flase
**/
function isOver()
{
if ($this->won)
return true;
if ($this->over)
return true;
if ($this->health < 0)
return true;
return false;
}
когда
timerComplete();
срабатывает в demo.js я хочу, чтобы вызвать функцию
конец()
в class.game.php, как я это сделать?
Вы должны прочитать об AJAX. –
Вы ищете Ajax-запросы? –
Я раньше не использовал AJAX, какой практический способ реализовать AJAX для этого решения? –