Decrease script or switch to PHP code, help

Asked

Viewed 84 times

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.

  • 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?

  • It depends on what you consider idle. Scroll, for example, without clicking anything, the guy is idle?

  • 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?

1 answer

2


Use 2 listeners and call the function to deploy this way:

deslogado = false;
function desloga(){
   temporiza = setTimeout(function(){
      alert("Deslogado por inatividade");
      deslogado = true;
   }, 5000);
}

document.body.onmousedown = function(){
   clearTimeout(temporiza);
   if(!deslogado){
      desloga();
   }
}

document.body.onscroll = function(){
   clearTimeout(temporiza);
   if(!deslogado){
      desloga();
   }
}

window.onload = desloga;
Clique na página ou faça scroll para não ser deslogado em 5 segundos
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />
<br /><br /><br /><br /><br /><br /><br /><br /><br />

  • Much more practical and without much redundancy, thank you very much for the clarity and patience in giving me a logic lesson, I’m starting ADS and I’m still a little lost in certain aspects of programming, can you tell me if this code is as secure in javascript as in PHP in relation to compatibility?

  • 1

    @netoschneider In php you won’t be able to do this, because php runs on the server. What you can do is save a SESSION with the time the guy logged in and then send an ajax to check the downtime. But that’s another question. Be sure to mark the answer with . If you want later I help with the PHP part.

  • @netoschneider Excludes the answer you posted tb because it is against the website regulation to post question issues or comments in the response field. Abs!

  • All right, already deleted, thank you very much for the information, just for giving this tip of the PHP area and simplify my code with these important tips already helped me up, thanks for the support and good night!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.