I am learning bootstrap carousels. But when I load the page into Firefox 72 / chromium / falkon but the interval doesn't work inside $( document ).ready(...)
, $(() = {...})
, but successfully works without them.
Code
<!Doctype HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Carousels</title>
<script src="js/jquery-3.4.1.min.js"></script>
<script src="bootstrap-3.3.4-dist/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="bootstrap-3.3.4-dist/css/bootstrap.min.css">
</head>
<body>
<div class="container-fluid">
<div class="carousel slide" id="myCarousel" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img src="images/a.jpg"/>
<div class="carousel-caption">First Slide</div>
</div>
<div class="item">
<img src="images/b.jpg"/>
<div class="carousel-caption">Second Slide</div>
</div>
<div class="item">
<img src="images/a.jpg"/>
<div class="carousel-caption">Third Slide</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
<script>
$( document ).ready(() => {
// works
window.console.log('hi')
// Doesn't work
$('.carousel').carousel({
interval: 100
})
})
</script>
</body>
</html>
This code has 2 images called a.jpg
and b.jpg
, which shows up correctly. But if I put comment out the $(document).ready()
the code works fine (changes from one image to other without sliding), although my instructor's code works fine within the $()
JQuery function!
Why doesn't the sliding interval works inside JQuery?