Reputation: 117
In a wordpress page. I have a tag that autoplays whenever the page is loaded. Everytime i resize the window the video restarts playing.I have a function that everytime if the url contains (/home/en) it changes the video (src) attribute, but for some reason, sometimes it doesn't.
jQuery(window).resize(function() {
if(window.location.href.includes("/home/en")){
jQuery('#testID').attr('src','https://test.mp4');
}
});
Does anyone have any idea what might be the problem!
Upvotes: 2
Views: 96
Reputation: 539
Can not tell you why it sometimes fails, except that your location
does not include what you want it to. Yet you only have to check it once.
//only need to check this once per window
var PlayVideo = window.location.href.includes("/home/en");
var PlayTimeout = null;
jQuery(window).resize(function(){
//adding a timeout to prevent multiple calls
window.clearTimeout(PlayTimeout);
PlayTimeout = window.setTimeout(function(){
if(PlayVideo){
jQuery('#testID').attr('src','https://test.mp4');
}
else{
//if you reach this point, your condition is not what you want it to be
console.log(window.location.href)
}
}, 1000)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Upvotes: 2