2
Good morning, if you don’t mind, I need help with a script I made in java.
<script type="text/javascript">
var data, horaAtual, tempoLimite;
var clique = 0;
function data() {
data = new Date();
horaAtual = data.getSeconds();
tempoLimite = horaAtual + 10;
}
var gravado = new Array();
gravado.push(tempoLimite);
function add() {
clique = 1;
}
function calcular() {
data;
document.body.onmousedown = add;
var numero = parseInt(gravado[0]);
var data2 = new Date();
var horaAtual2 = data2.getSeconds();
if((numero - horaAtual2) < 5){
alert('Tempo limite atingido!');
} if(clique == 1){
var data3 = new Date();
var horaAtual3 = data3.getSeconds();
var tempoLimite3 = horaAtual3 + 10;
gravado.splice(0);
gravado.push(tempoLimite3);
clique = 0;
alert(gravado[0]);
}
setTimeout(calcular,1000);
}
</script>
Well, let me explain a little bit of this mess, it is a very simple Script, it receives the current time from the computer and takes only the seconds, after that it generates another variable takes the second one more time and adds another 10s and writes in an Array (I had to do this because every time I upgraded it took a new value instead of keeping the old).
Then it subtracts the value of the Array with the current seconds so that a message is generated when the time reaches the limit that in the case is 5s.
Another function is that if the user clicks on one of the buttons of the site it generates a new time count and add more 5s updating the array.
Now you’re going to ask me why this mess, come on, I need a timeout system, so that the user has a time x to be on the site if he’s idle and that because of this inactivity he’s going to depress, but if it goes live within the time x the site manages the same x time so that the user is not logged.
I need to decrease this code, simplify this mess or exchange it for a PHP script, I tried to do it in PHP but it started getting too complex for me, I couldn’t find certain features, it was a failure!
I hope you understand my doubt, thank you in advance, thank you!
Very complicated. Instead of trying to explain your code, I think it would be better if you explained what you want to do, clearly, so we could propose a better and simpler code. I don’t understand why I took the current time of the computer, if, from what I understand, just see if the guy is 5 s inactive. For this the hour of the computer does not influence anything.
– Sam
Then would it be better to count backwards? Speaking this way I feel a little dyslexic, what would be the best way to create a code where the user is a time x idle on the site and in case he does not click any button is dropped?
– neto schneider
It depends on what you consider idle. Scroll, for example, without clicking anything, the guy is idle?
– Sam
I left my answer more balanced, I do not think it would be idle to be active looking at something on the page, but I do not know how to use it in codes, could help me?
– neto schneider