2015-03-31 123 views
-4

我怎么可以在我的js代码设置一个计时器...设置在JS定时器1秒钟

$("header").addClass("scrolled"); 

//INSERT TIMER FOR ONE SECOND HERE 

var newHeaderHeight = $("header").outerHeight(); 
$('#mainContent').css('padding-top', newHeaderHeight); 

我已经尝试了一些东西,但没有joy.eg:

$.doTimeout(1000, function() { 
       }); 
+4

这与Java有什么关系? – James 2015-03-31 16:15:12

+0

[使用setTimeout函数时出现问题]的可能重复(http://stackoverflow.com/questions/7679096/trouble-using-settimeout-function) – 2015-03-31 16:16:12

回答

0

使用JavaScript功能的setTimeout(函数,毫秒,可选的东西),要做到这一点:

$("header").addClass("scrolled"); 

setTimeout(function() { 
    // This will be executed after 1,000 milliseconds 
    var newHeaderHeight = $("header").outerHeight(); 
    $('#mainContent').css('padding-top', newHeaderHeight); 
}, 1000); 

演示:http://jsfiddle.net/BenjaminRay/hygnow06/

-1

您可以使用该功能setTimeout

它运行你必须在指定时间后提供的功能,它不会阻止该计划的其余部分。所以你可以试试:

setTimeout(function() { 
    // your code that should run after 1 second 
}, 1000;