问题
How to call a jquery function onload with some delay?
I want to call a function like this on load with some delay
$(".sample").live('click',function(){
var id=$(this).attr("id");
回答1:
This will wait 1 second and then assign the event handler...
function assignSampleClick() {
$(".sample").live('click',function(){
var id=$(this).attr("id");
});
}
// document ready
$(function() {
setTimeout(assignSampleClick, 1000);
});
回答2:
Try it like this..
$(document).ready(function () {
setTimeout(function () {
$(".sample").live('click',function(){
var id=$(this).attr("id");
}, 3000);
}, 3000);
This will fire on page load after 3 seconds delay.
回答3:
you could use setTimeout function you must refactor your code as something like this
//2 seconds wait before calling doJob
$(".sample").live('click',function(){ setTimeout(doJob, 2000);});
function doJob(){
var id=$(this).attr("id");
//other statements ...
}
回答4:
Try to use Timer Plugin
$(this).oneTime(1000, function() {
// some action here
});
回答5:
try this
var demora = window.setInterval(lerolero, 3000);
function lerolero() {
$("#ovo").css("display", "block");
}
来源:https://stackoverflow.com/questions/12930759/how-to-call-a-jquery-function-onload-with-some-delay