S
S
Space2014-03-09 12:49:21
JavaScript
Space, 2014-03-09 12:49:21

Why is the variable not changing?

Hello. There is a code that blocks the execution of an action if 3 seconds have not passed.
Timer so to speak.

var socketEmitLastCallTime = 0;
  var aqwsde = Date.now() - socketEmitLastCallTime ;
  if(aqwsde >= 3 * 1000) {
    socket.emit( 'message', { name: nameVal, message: msg, canal: canalVal } )
      socketEmitLastCallTime = Date.now();
   };
   alert(aqwsde);

Problem in what:
I deduce a variable alert'om aqwsde, and it deduces current time. Those. this variable socketEmitLastCallTimeis not stored. Why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yuri Lobanov, 2014-03-09
@ruslite

In connection with the clarifications:
Here is your function, for example, when you click on the button:
jsfiddle.net/iiil/FnbJn
You reset the socketEmitLastCallTime variable
each time. Accordingly, the click function works every time.
And here is an example jsfiddle.net/iiil/56n9s/1
We assign zero only when declaring, and do not reset when clicked. Accordingly, the alert is called only once every 3 seconds.

P
pomeo, 2014-03-09
@pomeo

Well, I mean, you

var socketEmitLastCallTime = 0;
var aqwsde = Date.now() - socketEmitLastCallTime;
alert(aqwsde);

you get the current time. I don't see any timer here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question