1
我正在嘗試使瀏覽器/計算機之間工作的基本計數器。 (我是新來的)。該按鈕只能工作大約1/12時間。我如何使它更一致?按鈕更新node.js和HTML
謝謝!
我的服務器端node.js的樣子:
var express = require('express');
var app = express();
var hits = 0;
app.get('/main.html', function (req, res, next) {
console.log('bye');
next();
});
app.get('/', function (req, res, next) {
console.log('hello');
next();
});
app.get('/add', function (req, res) {
console.log('added hits');
hits++;
});
app.get('/hitNum', function (req, res) {
console.log('hits req');
res.end(hits+'');
});
app.use(express.static(__dirname + '/public'));
var server = app.listen(80);
我的HTML看起來像:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var add = function() {
$.get('add');
}
window.setInterval(function(){
$.get('hitNum').done(function (data){
document.getElementById('hits').innerText = data;
});
}, 500);
</script>
</head>
<body>
<button onclick='add()' >Hi</button>
<p id='hits'></p>
</body>
</html>
它的工作原理!非常感謝! – Bluedog111 2014-11-23 00:13:32