Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Michael Thomas
10,910 PointsHow do I get this jQuery function to accept arguments?
- I have two video tags with ids #video and #video2
- I have two divs with ids #preloader and #preloader2
How do I write this jQuery function so that I can just pass it parameters instead of having to rewrite the function twice? Here's what I have working for #video and #preloader...
var $video = $('#video');
var $preloader = $('#preloader');
function checkLoad(){
if($video[0].readyState === 4){
$preloader.addClass('loaded');
$video.addClass('loaded');
}else{
setTimeout(checkLoad, 100);
}
}
checkLoad();
1 Answer
LaVaughn Haynes
12,397 PointsHere's a working version. You had a few small mistakes. Run it in a browser. The red boarder will turn green after the video loads
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Video</title>
<style>
video, div{
border: 2px solid red;
}
video.loaded, div.loaded{
border: 2px solid green;
}
</style>
</head>
<body>
<div id="throbber">THIS WILL DISAPPEAR...</div>
<video id="video" controls width="560">
<source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
<source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
<source src="http://techslides.com/demos/sample-videos/small.3gp" type="video/3gp">
</video>
<div id="throbber2">THIS WILL ALSO DISAPPEAR...</div>
<video id="video2" controls width="560">
<source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
<source src="http://techslides.com/demos/sample-videos/small.ogv" type="video/ogg">
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
<source src="http://techslides.com/demos/sample-videos/small.3gp" type="video/3gp">
</video>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
var $video = $('#video');
var $preloader = $('#throbber');
var $video2 = $('#video2');
var $preloader2 = $('#throbber2');
function checkLoad(myVideo, myPreloader){
//use [0] to get the video element
if(myVideo[0].readyState === 4){
//SPELLING TYPO => "OA" not "AO"
//myPrelaoder.addClass('loaded');
myPreloader.addClass('loaded');
myVideo.addClass('loaded');
}else{
//USE ANONYMOUS FUNCTION TO PASS SCOPE VARIABLES BACK TO FUNCTION
//setTimeout(checkLoad, 100);
setTimeout(function(){
checkLoad(myVideo, myPreloader);
}, 100);
}
}
//CHANGE THE LINES BELOW TO PASS THE OUTSIDE VARIABLES INTO THE FUNCTION
//checkLoad(myVideo, myPreoader);
checkLoad($video, $preloader);
//checkLoad(myVideo2, myPreoader2);
checkLoad($video2, $preloader2);
</script>
</body>
</html>
Michael Thomas
10,910 PointsMichael Thomas
10,910 PointsThanks again LaVaughn, I've finished for the day but I'll try this tomorrow!