我有一个代码,可以计算从点击到现在页面所花费的时间,我的问题是如何记录单击“提交”按钮并将其发送到数据库的时间
< script >
var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);
function setTime() {
++totalSeconds;
secondsLabel.innerHTML = pad(totalSeconds % 60);
minutesLabel.innerHTML = pad(parseInt(totalSeconds / 60));
}
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
</script>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<label id="minutes">00</label>:<label id="seconds">00</label><label id="houres">:00</label><li>
</ul>
</div>
<input type="submit" name="register" class="btn btn-info" value="Submit" />
答案 0 :(得分:0)
尝试分解问题。首先分析您的程序流程:
当前,每秒(1000
毫秒):
setTime
函数被称为( update )totalSeconds
((数据)模型/状态)pad
(视图模型)现在,您要将一些数据发送到数据库...让我们分解一下:
上面提到的流程在Web开发的世界中很普遍,更普遍的是:
按照此模式,可以重写代码:
<span class="time">
<span class="mm"></span>:<span class="ss"></span>
</span>
<button class="submit-time">Save time</button>
<script>
const view = {
mm: document.querySelector('.time .mm'),
ss: document.querySelector('.time .ss'),
submit: document.querySelector('.submit-time'),
}
const render = ()=> {
const viewModel = viewModelFromState(state)
view.mm.innerText = viewModel.mm
view.ss.innerText = viewModel.ss
}
const viewModelFromState = state=> ({
mm: pad(Math.floor(state.secondsTotal / 60)),
ss: pad(state.secondsTotal % 60),
})
const state = {
secondsTotal: 0,
}
const update = ()=> {
state.secondsTotal += 1
}
const eventListeners = {
submit: ()=> {
alert('value is '+state.secondsTotal)
const onSuccess = (text)=> {
alert("Saved! "+text)
}
const onFail = (error)=> {
alert("Couldn't save!\n" + error)
}
saveSecondsTotal(state.secondsTotal)
.then(res=> res.text()).then(onSuccess)
.catch(err=> onFail(err))
},
}
const setupEventListeners = ()=> {
view.submit.addEventListener('click', eventListeners.submit)
}
// api
const saveSecondsTotal = (secondsTotal)=> {
const val = ''+secondsTotal // explicitly convert number to string (unnecessary)
const url = '/save-seconds-total'
const queryString = 'val='+val
const fullUrl = url+'?'+queryString
return fetch(fullUrl)
}
// helpers
const pad = (val)=> {
const str = ''+val
return str.length < 2? '0' + str: str
}
// start
setupEventListeners()
setInterval(()=> {
update()
render()
}, 1000)
</script>
我通过删除未使用的内容(例如小时和一些标记)来“简化”示例。我还使用了一些语言功能,例如箭头功能,对象,const,三元运算符,promise等,这些功能可能看起来不错。
尽管如此,对于此示例,“简单”的显式解决方案可能是:
<span>
<span id="mm"></span>:<span id="ss"></span>
</span>
<button onclick="submitTime">Save time</button>
<script>
// state + render
var mmElement = document.getElementById('mm')
var ssElement = document.getElementById('ss')
var secondsTotal = 0
function update () {
secondsTotal += 1
var mm = pad(Math.floor(secondsTotal / 60))
var ss = pad(secondsTotal % 60)
mmElement.innerText = mm
ssElement.innerText = ss
}
// submit
function onSubmitSuccess (text) {
alert("Saved! "+text)
}
function onSubmitFail (error) {
alert("Couldn't save!\n" + error)
}
function saveSecondsTotal (secondsTotal) {
return fetch('/save-seconds-total?val='+secondsTotal)
.then(function (response) {return response.text()})
}
function submitTime () {
alert('value is '+secondsTotal)
saveSecondsTotal(state.secondsTotal)
.then(onSuccess)
.catch(onFail)
}
// helpers
function pad (val) {
var str = ''+val
if (str.length < 2) {
return '0' + str
}
return str
}
// start
setInterval(update, 1000)
</script>
在这里,回答您的问题的有趣部分是“提交”部分。 PHP方面看起来像:
<?php // in http-root/save-seconds-total.php
$val = $_GET["val"];
$secondsTotal = intval($val);
// ... database specific code ...
echo "Saved ".$secondsTotal." seconds!"
?>
我建议您阅读并阅读更多示例/教程。在此处提问时,请显示您尝试过的内容以及可能的假设。尝试去除所有不必要的细节,以获取问题的核心。希望您问的原因是您对理解感兴趣,而不是因为您想为您完成一些功课...