简体   繁体   English

如何在slidetoggle()上停止setInterval()然后再次重新启动?

[英]how to stop setInterval() on slidetoggle() then restart again?

I have looked at all the other answers similiar to this question but i can't make them fit my scenario. 我已经看过所有其他与此问题类似的答案,但我无法使其适合我的情况。

i have a page that has a sidebar that contains 15 results that auto refreshes, which is fine. 我有一个页面的侧边栏包含15个自动刷新的结果,这很好。 There is a link that loads up an external page into a div with an overlay. 有一个链接可将外部页面加载到带有叠加层的div中。 then this new div with the FULL list of the results also auto refreshes whilst its open. 那么带有结果列表FULL的新div在打开时也会自动刷新。 (don't want both divs auto-refreshing in background uneccesarily) (不要让两个div都在后台自动刷新)

in this full list div each result has a jquery slideToggle() function to display more information. 在此full list div中,每个结果都有一个jquery slideToggle()函数以显示更多信息。

what i am TRYING(and failing) to do is make the auto refresh stop whilst this slideToggle is displaying information, cos otherwise when the page refreshes it goes back to display:none . 我正在尝试(并且失败)的操作是在此slideToggle显示信息时使自动刷新停止,否则,因为页面刷新时它会返回display:none

here is my latest attempt: 这是我最近的尝试:

html html

main page has div to load into...
<div id="full_list"> loading... </div>

external page with the full list 具有完整列表的外部页面

<div class="events_list">          <!--container for events -->
<ul><li>                           
<div class="full_event">           <!--each event in a list-->
#1<b> 500 m race </b>              <!--this bit always seen -->

<div class="event_slide">         <!--this div is slideToggled() -->
<b>500m race </b><br>
<div class="evntdescription"> 
run 500mrun 500mrun 500mrun 500mrun 500mrun 500m
</div>
</div>
</div>
</li></ul>
</div>

now my attempted javascript 现在我尝试的JavaScript

var refreshData;

var infodisplay = $('.event_slide').css('display');

function autoRefresh () {

            if(infodisplay == 'none')
              {
            refreshData = setInterval(function() {          
                    $('#full_list').load('eventlist.php');
                    }, 1000);
              }     
            else
              {
            clearInterval(refreshData);
              }

};      

$("#view_events").click(function(){        //this opens up the div //
        overlay.fadeIn(1000).appendTo(document.body);
        $('#full_list').load('eventlist.php'); //loads external page//
        $('#full_list').fadeIn(800, function() {

        autoRefresh ();    //start the auto refresh/

        $("body").on("click",".full_event", function(e){ 

        $(this).children('div.event_slide').slideToggle(300, function() {

        autoRefresh ();   //thought it might work if i add the function   
                                      //here as well //

        });
        });

        });

        $("body").on("click","#close", function(e){ //closes div//
            overlay.fadeOut(300);
            $("#full_list").fadeOut(300);
            }); 

        return false;
        });

basically it doesnt do anything. 基本上它什么都不做。 i have made it work if i get rid of the second autoRefresh function, but it won't stop the function from going. 如果我摆脱了第二个autoRefresh函数,就可以autoRefresh工作,但是它不会阻止该函数的运行。 just keeps on refreshing, also not sure how to stop the refresh when i close the div aswell. 只是不断刷新,也不确定在关闭div时如何停止刷新。 let me know if you need more info. 让我知道您是否需要更多信息。

thanx! 谢谢!

Try clearing the interval when the asynchronous loading has completed: 尝试清除异步加载完成后的时间间隔:

function autoRefresh () {
    refreshData = setInterval(function() {
    $('#full_list').load('eventlist.php', function() {
        clearInterval(refreshData);
    });
}, 1000);  };

ok so i figured it out anyway. 好的,所以我还是想通了。 thanx for trying if you did tho. thanx尝试如果您做了。

put the if statement into a function after toggle is completed. 切换完成后,将if语句放入函数中。

only downside is if it refreshes at same time that its still 'toggling' it will refresh. 唯一的缺点是,如果在仍然“切换”的同时刷新,它将刷新。 it won't clear the interval untill the toggle has completed. 在切换完成之前,不会清除间隔。 no idea how to get round that. 不知道该如何解决。 changed the code to this: 将代码更改为此:

var refreshData;

function autoRefresh() {            
       refreshData = setInterval(function() {          
                $('#full_list').load('eventlist.php');
                }, 10000);
                };   

$("#view_events").click(function(){        //this opens up the div //
    overlay.fadeIn(1000).appendTo(document.body);
    $('#full_list').load('eventlist.php'); //loads external page//
    $('#full_list').fadeIn(800, function() {

    autoRefresh ();    //start the auto refresh/

    $("body").on("click",".full_event", function(e){ 

    $(this).children('div.event_slide').slideToggle(300, function() {

     if($(this).css('display') == 'none') 
        {$('#full_list').load('eventlist.php');
        autoRefresh();
        }
        else
        {
        clearInterval(refreshData);
        };

    });
    });

    });

    $("body").on("click","#close", function(e){ //closes div//
        overlay.fadeOut(300);
        $("#full_list").fadeOut(300);
        }); 

    return false;
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM