We’re building out a prototype for a website navigation and are struggling to find a solution to a problem we have.
We have tested the navigation prototype with customers and found that customers would accidentally hover over different navigation tabs while already in a dropdown, which would change to a new dropdown unintentionally.
We think a good solution for this is to add a delay timer before the any additional navigation dropdown opens.
So as an example, a user hovers over "A. underwear and sleepwear" then hovers over "B. Activewear", we add a 1 second delay so that (A) will wait 1 second before dissapearing and showing (B). But if the user goes back to (A) in under 1 second then (B) will not open.
I’ve created a screen recording with audio to describe the problem better:
https://www.loom.com/share/7dc9df6299754a4595649b1214da19b5
This is the Jquery code we are using for the hover function:
$("ul#subnav-container li").hover(
function () {
// syncWait(1001);
$(this).addClass("active");
},
function () {
// syncWait(1000);
$(this).removeClass("active");
}
);
We do not know how to write up the "syncWait" function. Any help is appreciated.
We tried to add a setTimeout function but when hovering on other tabs it would wait 1 second before still showing all the other tabs.
2
Answers
Try
setTimeout
andclearTimeout
There’s no delay or sleep in Javascript for performance reasons. But you can use
setTimeout()
instead.For example you can create a lock flag in your code: