Adeel Aslam
Adeel Aslam

Reputation: 1294

javascript declare function with timeout

I have javascript like and i want to set time delay to this function parameters.here is my simple function

function mopen(id)
    {   

        mcancelclosetime();

        if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
        ddmenuitem = document.getElementById(id);
        ddmenuitem.style.visibility = 'visible';

    }

and here i want to set a timeout delay with id.I mean

the id should open with a time delay like this

function settimeout(mopen(id),1000)
{
}

But My this try is not working please help me how can i do this

Update

here is my loop function

 function hideshow(span) {
        hideDiv();
        var div = document.getElementById("DIV_" + span.id);
        if (div.style.display == "none")
        setTimeout(div.style.display = "block",2000);
        else
            div.style.display = "none"; 
    }

can u please now tell me how to settimeout for the div to become a block.I have set it but its not working

Upvotes: 0

Views: 1008

Answers (3)

Cheery
Cheery

Reputation: 16214

The answer was already given (not in the way you expected, I suppose). Rewrite your function as

 function hideshow(span) {
    hideDiv();
    var div = document.getElementById("DIV_" + span.id);
    if (div.style.display == "none")
        setTimeout(function() { div.style.display = "block" }, 2000);
    else
        div.style.display = "none"; 
  }

Upvotes: 0

Rishav Rastogi
Rishav Rastogi

Reputation: 15492

That won't work, you can simply do this. In your case your defining a function called setTimeout, which is not necessary, this method is already available in the browser's Javascript standard library.

 setTimeout( function(){mopen(id)}, 1000);

Generally you can just do

 setTimeout( yourFunction, 1000);

which works perfectly, but when you have to pass parameters to a function in setTimeout, you can use first version, as in call it inside an anonymous "function () {}" call.

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270607

Call mopen() in an anonymous function passed to setTimeout().

var t = setTimeout(function() {
  mopen(id)
}, 1000);

Upvotes: 1

Related Questions