Reputation: 41
How To call a javascript url after 5 seconds
Slider Example :
$(document).ready(function() {
$('.slider').bxSlider();
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.min.js"></script>
<div class="slider">
<div>I am a slide.</div>
<div>I am another slide.</div>
</div>
I want to call and execute this two js after 5 seconds is there any possibility ?
Upvotes: 0
Views: 375
Reputation: 291
If you want to redirect to some other page after a period of time this might help you:
$(document).ready(function(){
setTimeout(function () {
window.location.href = 'http://example.com';
}, 5000);
});
Upvotes: 0
Reputation: 65806
$(document).ready(function() {
// After the document is ready, wait 5 seconds and run the function
// supplied to setTimeout
setTimeout(function(){
$('.slider').bxSlider();
}, 5000);
});
<link rel="stylesheet" href="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/bxslider/4.2.12/jquery.bxslider.min.js"></script>
<div class="slider">
<div>I am a slide.</div>
<div>I am another slide.</div>
</div>
Upvotes: 0
Reputation: 14863
It would be a much better idea to execute the functionality after n seconds instead of waiting to load the external files.
$(document).ready(function(){
setTimeout(function () {
$('.slider').bxSlider();
}, 5000);
});
There is no way to guarantee the scripts being loaded within a certain period of time. There could also be some odd handling if the files are already locally cached by the user. Your question is a typical XY problem, where you ask about the attempted solution instead of the actual problem.
Upvotes: 3
Reputation:
Try this:
setTimeout( function(){
$('.slider').bxSlider();
}, 5000 );
Upvotes: 0
Reputation: 16997
use setTimeout()
- calls a function or evaluates an expression after a specified number of milliseconds.
setTimeout( function(){
// say 5 seconds
// call your url here
// or put your code, which need to be executed after x seconds
}, 5000 );
Upvotes: 1