Need help synchronizing events with jquery

Here is my code:

tripper = 2; $("#topheader").mousewheel(function(event, delta) { if (tripper == 2){ startPlace = $("#content").scrollLeft(); startCounter = something; tripper = 1; } else { currentPlace = $("#content").scrollLeft(); if(startCounter < 100){ // milliseconds distanceMoved = currentPlace - startPlace; if(distanceMoved > 100){ slideRight(); } else if(distanceMoved < -100){ slideLeft(); } } else { tripper = 2; } } } 

What is the correct way to check if 100 milliseconds have passed for the first time with this function? In the 5th line of code, I have a variable "something" that needs to be replaced with some kind of counter. Or maybe I'll end this completely wrong. Suggestions?

+6
source share
2 answers

You can create an instance of the Date object as follows:

 var then = new Date(); 

You can do one more later:

 var now = new Date(); 

Subtraction gives the difference in milliseconds:

 var elapsed = now - then; 

(Forcing from "Date" to "Number" is implicit when two date values ​​appear on both sides of the subtraction operator. The conversion is similar to calling "now.getTime ()".)

+5
source

The following code is untested, but basically after 100 milliseconds it should reset the timeout back to zero and eventually set the tripper back to 2;

 tripper = 2; timeout = null; $("#topheader").mousewheel(function(event, delta) { if (tripper == 2){ startPlace = $("#content").scrollLeft(); if (!timeout) { setTimeout(function() { timeout = null }, 100); } tripper = 1; } else { currentPlace = $("#content").scrollLeft(); if(timeout){ // milliseconds distanceMoved = currentPlace - startPlace; if(distanceMoved > 100){ slideRight(); } else if(distanceMoved < -100){ slideLeft(); } } else { tripper = 2; } } } 
+3
source

Source: https://habr.com/ru/post/889024/


All Articles