Jquery delay execution of script

Having the following:

$('#navMain .nav1').hover(function () {
    $(this).addClass('hover');
    if ($.browser.msie && $.browser.version < 7) 
        $('select').css('visibility', 'hidden');
}, function () {
    $(this).removeClass('hover');
    if ($.browser.msie && $.browser.version < 7) 
        $('select').css('visibility', 'visible');
});

I need to delay the executing of addClass('hover') after hovering over $('#navMain .nav1') element.

I've tried this but it doesn't work:

$(this).delay().addClass('hover');
if ($.browser.msie && $.browser.version < 7) 
    $('select').delay().css('visibility', 'hidden');

thanks

Answers:

Answer
setTimeout( function(){

  // your stuff here

}, 500); // delay 500 ms

with your code:

$('#navMain .nav1').hover(

  function () {
    setTimeout( function(){
        $(this).addClass('hover');
        if ($.browser.msie && $.browser.version < 7) $('select').css('visibility', 'hidden');
    }, 500); // delay 500 ms
  }, 

  function () {
    $(this).removeClass('hover');
    if ($.browser.msie && $.browser.version < 7) $('select').css('visibility', 'visible');
  }

);
Answer

If a simple javascript delay can help you, you can do as follow:

setTimeout(function() {
  // your code here
}, 3000);

With the following signature: setTimout(functionToExecute, delayInMs);

and the documentation: http://www.w3schools.com/js/js_timing.asp

Hopping I help you.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.