Javascript setTimeout or jquery delay - don't work for me
I have a div like this
<div id="sale"> ........ </div> and I tried to use both
$('#sale').delay(3000).slideDown(500); and
setTimeout(sale(), 3000); function sale() { $('#sale').slideDown(500); } but none of them work. Delay jQuery says $('#sale').delay() not a function, while the setTimeout says useless calling setTimeout (missing quotes). If I add double quotes around the call to sale() , it simply says: "Sale is undefined."
Why are none of them working?
All I'm trying to do is make a div in 3 seconds after the page loads.
In the case of setTimeout you are just doing it wrong.
setTimeout(sale(), 3000); // will call sale and use the RETURN value in the callback but sale returns undefined You need to pass a function:
function sale() { $('#sale').slideDown(500); } setTimeout(sale, 3000); // just pass in the reference to sale() Another possibility:
// no difference in this case // Note: if it were obj.sale() then you would need to do this version // otherwise sale() will get called with the this set to window setTimeout(function(){sale()}, 3000) And last but not least:
setTimeout(function() { $('#sale').slideDown(500); }, 3000); You need to be in the queue for delay() .
$('#sale').queue(function() { $(this).delay(3000).slideDown(500).dequeue(); }); Patrick Dw said in the comments that you do not need to be in queue() if your next chain of methods is animation. See Its JSFiddle .
setTimeout(sale, 3000); Before that, you passed setTimeout return value from sale . This conveys a valid function.
In your first solution, it seems that jQuery is not even loaded.
In the second code, you should do setTimeout(sale, 3000); (omit the parentheses) because with them you call setTimeout with return sale() , which is undefined .
setTimeout("sale();", 3000);