我正在使用Node.js
和Socket.IO
所有功能都可以在浏览器上完美运行。
var socket = io.connect('http://<?php echo $dAndonAccessDtl['IP_ADDRESS']; ?>:8080');
window.addEventListener("load", function()
{
var lightbox = document.getElementById("light");
lightbox.addEventListener("change", function()
{
socket.emit("light", Number(this.checked));
$("#value").val(data);
var value = $("#value").val();
var total = $("#total").val();
var grandtotal = parseInt(value) + parseInt(total);
$("#total").val(grandtotal);
var runningQty = $("#runningQty1").val();
$('.getIndicator').data('radialIndicator').animate(grandtotal).option('maxValue',parseInt(runningQty));
});
});
socket.on('light', function (data)
{
document.getElementById("light").checked = data;
socket.emit("light", data);
var cavity = $("#cavity1").val();
$("#value").val(data * cavity);
var value = $("#value").val();
var total = $("#total").val();
var grandtotal = parseInt(value) + parseInt(total);
$("#total").val(grandtotal);
var runningQty = $("#runningQty1").val();
var planID = $("#planID").val();
$('.getIndicator').data('radialIndicator').animate(grandtotal).option('maxValue',parseInt(runningQty));
if(data == 1)
{
$.ajax(
{
type: "POST",
url: "saveQuantity",
data: 'planID=' + planID + '&quantity=' + data * cavity,
success: function(msg)
{
}
});
}
});
当前情况,JS将在页面加载到浏览器后运行。
现在我需要使用按钮运行JS时控制它吗?
$('.startProduction').on('click', function()
{
$('.startProduction i').toggleClass('glyphicon-play glyphicon-pause');
var a = $('.startProduction i');
a.siblings().removeClass();
if(a.hasClass('active'))
{
a.removeClass('active').toggleClass('inactive');
//What function to stop the JS?
}
else
{
a.removeClass('inactive').toggleClass('active');
var socket = io.connect('http://<?php echo $dAndonAccessDtl['IP_ADDRESS']; ?>:8080');
window.addEventListener("load", function()
{
var lightbox = document.getElementById("light");
lightbox.addEventListener("change", function()
{
socket.emit("light", Number(this.checked));
$("#value").val(data);
var value = $("#value").val();
var total = $("#total").val();
var grandtotal = parseInt(value) + parseInt(total);
$("#total").val(grandtotal);
var runningQty = $("#runningQty1").val();
$('.getIndicator').data('radialIndicator').animate(grandtotal).option('maxValue',parseInt(runningQty));
});
});
socket.on('light', function (data)
{
document.getElementById("light").checked = data;
socket.emit("light", data);
var cavity = $("#cavity1").val();
$("#value").val(data * cavity);
var value = $("#value").val();
var total = $("#total").val();
var grandtotal = parseInt(value) + parseInt(total);
$("#total").val(grandtotal);
var runningQty = $("#runningQty1").val();
var planID = $("#planID").val();
$('.getIndicator').data('radialIndicator').animate(grandtotal).option('maxValue',parseInt(runningQty));
if(data == 1)
{
$.ajax(
{
type: "POST",
url: "saveQuantity",
data: 'planID=' + planID + '&quantity=' + data * cavity,
success: function(msg)
{
}
});
}
});
}
});
如您所见,我设置了JS将在单击按钮.startProduction i
之后运行。在此之前,它仍然有效。
但是当我尝试停止它时(单击相同的按钮),直到这之前我都不知道该怎么做。
我的问题是否有可能在浏览器上停止nodejs和socket.io的设置间隔?然后再次返回以开始?
答案 0 :(得分:0)
是的,可以做到
//声明一个计时器
var nrgstimer = [];
//安排一个计时器
nrgstimer [socket.room] = setTimeout(function(){
//Your code ...
},20000);
//删除计时器
if(!isUndefinedVar(nrgstimer [socket.room])){
clearTimeout(nrgstimer[socket.room]);
}