[英]why javascript function stops working after returning back?
i have made a rotating slider in javascript and css. 我已经在javascript和CSS中制作了一个旋转滑块。 Javascript function makes it rotating. Javascript函数使其旋转。 When i first time load the page it works fine but when i navigate to some other page and then return back, the js function is called but the slider does not stop rotating on mouse hover. 当我第一次加载页面时,它工作正常,但是当我导航到其他页面然后返回时,调用了js函数,但是滑块不会随着鼠标悬停而停止旋转。 I have made a js function that when mouse hovers on div the rotation stop, when it leaves the function is called again.Here is my js code 我做了一个js函数,当鼠标悬停在div上时,旋转停止,离开它时再次调用该函数。这是我的js代码
var timer;
window.onload = function()
{
myTimer();
};
var $i=1;
function myTimer(){
timer=setInterval(function(){
if($i<6){
closeTab();
openTab($i);
$i++;
}
else
{$i=1;}
},2000);
}
closeTab();
var selectedSlider ;
function closeTab()
{
var $this = $('#accordion > li');
//$width=$(window).width();
//$this.stop().animate({'width':'110px'},500);
$this.stop().animate({'width':'7%'},500);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
}
function openTab(id)
{
var $this = $('.bg'+id);
//$this.stop().animate({'width':'450px'},500);
$this.stop().animate({'width':'70%'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
}
openTab(1);
$(function() {
$('#accordion > li').hover(
function () {
console.log("in mouse hover");
var $this = $(this);
selectedSlider = $this.attr("class").replace("bg", "");
closeTab();
openTab(selectedSlider);
clearInterval(timer);
},
function () {
}
);
});
$('#accordion > li').mouseleave(function(){
console.log("in mouse leave");
myTimer();
});
As @David said browser was caching the javascript, so your code for hover effect is not work as you put it in such a way, that when js file will call then hover event is fire but here js file is already cached. 正如@David所说的,浏览器正在缓存javascript,因此,用于悬停效果的代码无法像您所说的那样起作用,即当js文件将被调用时,悬停事件被触发,但是js文件已经被缓存了。
You may try this, instead of using hover function anonymously put it inside window.onload
. 您可以尝试这样做,而不是使用悬停功能匿名将其放在window.onload
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.