clearInterval,在鼠标悬停时停止计时器

时间:2017-07-24 19:45:30

标签: javascript timer setinterval mouseover clearinterval

我在过去的几个月来回试图解决这个问题,但我无处可去,不管我的尝试...我终于在我的机智结束,真的需要帮助我害怕我会松动意志继续..

如果任何人都可以看到我的错误/尽管除了图像本身没有被明显显示之外没有任何明显的图像显示在html页面中,如果有人能帮助我解决这个小但令人沮丧的异常,我会很高兴。 ..我提前谢谢你们..

这里是代码....我发现我确实可以从html页面调用已停止的函数。我的解决方案我以前从未尝试过。

var tim=8000



function stopper (){

clearInterval(foot);


console.log (tim)
}



function fader (){


var element = document.getElementById("dynamic-image2");
element.style.transitionDuration = "2s";
<!---the grabbed element + style [ execute type of effect ] = ' dimension of effect (1 ,2)';--->
 element.style.opacity=4;
}

function fader2 (){

var element = document.getElementById("dynamic-image2");
element.style.transitionDuration = "2s";
<!---the grabbed element + style [ execute type of effect ] = ' dimension of effect (1 ,2)';--->
 element.style.opacity=0;
}

   var timer=tim
   var timer2=timer / 2

setInterval( fader2,timer2 );

(function() {
   'use strict';



   var preloads=[],c,
       d=document,di=d.getElementById('dynamic-image2'),
       dh=d.getElementById('dynamic-href2'),
       url=[
           'linkmywebsite.ml','chatmagbingo.com','ilkestonwebdesign.com','ipost.com','happypetsuk.com','www.linkmywebsite.co.uk','fireworksnottingham.co.uk','www.linkmywebsite.co.uk','ilkestonfastfood.com','www.linkmywebsite.co.uk','de-signz.co.uk','www.linkmywebsite.co.uk','cctautos.com','grundyskips.co.uk','www.m8drivingschools.co.uk','www.apipedream.co.uk','www.crazyskips.com','www.linkmywebsite.co.uk','www.linkmywebsite.co.uk','www.linkmywebsite.co.uk','www.linkmywebsite.co.uk','www.linkmywebsite.co.uk','www.competitiveremovals.co.uk','www.linkmywebsite.co.uk','www.linkmywebsite.co.uk','heartinternet.co.uk','winnerbingo.com','www.linkmywebsite.co.uk','linkmywebsite.ml','katyperry.com','perfectpizza.com','youtube.com','google.com','facebook.com','twitter.com','linkmywebsite.ml','linkmywebsite.ml','gumtree.com'
          ];

        /*adjust to suit */






function preload(){
for(c=0;c<arguments.length;c++) {
   preloads[preloads.length]=new Image();
   preloads[preloads.length-1].src=arguments[c];
  }
   c=0;

 }


preload('yournewsite2.png','1ad.jpg','2ad.jpg',
'3ad.jpg','4ad.jpg','5ad.jpg',
'6ad.jpg','7ad.jpg','8ad.jpg',
'9ad.jpg','10ad.jpg','11ad.jpg',
'12ad.jpg','13ad.jpg','14ad.jpg',
'15ad.jpg','16ad.jpg','17ad.jpg',
'18ad.jpg','19ad.jpg','20ad.jpg',
'21ad.jpg','22ad.jpg','23ad.jpg',
'24ad.jpg','25ad.jpg','26ad.jpg',
'27ad.jpg','28ad.jpg','29ad.jpg',
'30ad.jpg','youtubead.png','googlead.png',
'facebookad.png','twitterad.png','yournewsite2.png',
'yournewsite2.png','gumtreead.png');




setInterval( fader, timer );

const foot = setInterval(     function(){

   dh.href='http://'+url[c];
   di.src=preloads[c].src;
   di.alt=preloads[c].src.substring(preloads[c].src.lastIndexOf('/')+1);
   c++;
if(c==preloads.length) {
   c=0;
  }
 },timer);



}())

1 个答案:

答案 0 :(得分:1)

您需要做的是将间隔存储在变量中,然后将其作为参数传递给clearInterval。

所以作为一个最小的例子

const foo = setInterval(func, 1000);
clearInterval(foo);