I want to check $(document).scrollTop()
two seconds after the page loads. How can I do that? Can somebody help me? I am newbie in jQuery?
I tried this, but it does not work:
$(document).delay(2000).scrollTop()
I want to check $(document).scrollTop()
two seconds after the page loads. How can I do that? Can somebody help me? I am newbie in jQuery?
I tried this, but it does not work:
$(document).delay(2000).scrollTop()
Share
Improve this question
edited Dec 24, 2014 at 13:36
Mr. Polywhirl
48.9k12 gold badges93 silver badges145 bronze badges
asked Dec 24, 2014 at 13:34
nowikonowiko
2,5777 gold badges40 silver badges87 bronze badges
3
-
6
setTimeout
is your fiend. – Ram Commented Dec 24, 2014 at 13:36 -
use
setTimeout
rather than delay – akaBase Commented Dec 24, 2014 at 13:36 -
1
Remember that
.delay()
can only be used for queued effects in jQuery. It is not intended as a replacement forsetTimeout()
. – Terry Commented Dec 24, 2014 at 13:40
2 Answers
Reset to default 9You can do it using the setTimeout() function after the document.ready
was triggered:
$(function(){ //wait for document ready
setTimeout(function(){
$(document).scrollTop()
}, 2000) //execute your function after 2 seconds.
});
The first step is to fire an event handler when the document is loaded. That event is the document ready event.
The second step is to create a timeout that will execute the required function after a specified delay.
var scrollTimeout = null;
var jDocument = $(document);
jDocument.ready(function () { //An event fired when the document finishes loading.
scrollTimeout = setTimeout(function () { //A function the will be executed after 2000ms
jDocument.scrollTop(); //your code goes here.
}, 2000);
});