服务器上有一个计时器,当它启动时,就开始倒计时,但是当用户离开页面时,计时器继续运行,并在用户不再在页面上时触发。如何在用户离开页面之前或已经离开时停止此计时器?
//Timer function
function startTimer() {
console.log("Timer statrted!");
var countdown = 15000;
setTimeout(function () {
console.log("Timer done!");
}, 15000);
}
socket.on("get data", function() {
io.to(room_name).emit("data");
//call timer
startTimer(); //"Timer started!", "user disconnected", (after 15s) "Timer done!"
});
socket.on("disconnect" ,function(){
console.log("user disconnected");
});
Run Code Online (Sandbox Code Playgroud)
我试图停下clearTimeout()来socket.on("disconnect"),socket.on("disconnecting") 但它们对已经离开页面的当前用户不起作用...它们只为其他用户触发...
我正在学习如何使用Node.js和React生成API,来自这个源代码: link
getLangData: function(e) {
e.preventDefault();
var lang = this.refs.lang.value;
fetch("/api/getLangData?lang="+lang)
.then(function(data) {
return data.json();
})
.then(json => {
this.SetState({
langData: json
})
})
}
Run Code Online (Sandbox Code Playgroud)
我有下一个错误:
SyntaxError: http://localhost:3000/js/api.js: Unexpected token (28:26)
26 | },
27 | getLangData: function(e) {
> 28 | e.preventDefault();
| ^
29 | var lang = this.refs.lang.value;
30 |
31 | fetch("/api/getLangData?lang="+lang)
Run Code Online (Sandbox Code Playgroud)
怎么样 ";" 可以是意外的令牌吗?我的代码出了什么问题?
我的完整代码:https://jsfiddle.net/s7xdbrcw/
我正在使用 node.js/cheerio(与 jQuery 语法相同)来解析页面。当我使用时:
$ = cheerio.load(body);
console.log($('.td-title a').text());
Run Code Online (Sandbox Code Playgroud)
我的控制台中有很长的字符串,来自单词“mainaboutcontactusprofile”等。如何制作一组 td 文本?
javascript ×3
node.js ×2
api ×1
cheerio ×1
jquery ×1
reactjs ×1
socket.io ×1
timer ×1
web-scraping ×1