简体   繁体   English

如何冻结Javascript超时和BS4 / jQuery动画?

[英]How to freeze Javascript timeouts and BS4/jQuery animations?

I have an HTML page that has timeouts. 我有一个超时的HTML页面。 I want to freeze them when you press a button ( #pauseButton ) and then resume when you press it again, preferably freezing all BS4 and jQuery animations also. 我想在按下按钮( #pauseButton )时冻结它们,然后在再次按下时恢复它们,最好还冻结所有BS4和jQuery动画。

<button id="pauseButton"></button>
<script>
$(document).ready(function(){
    setTimeout(function() {
        alert("This is an alert")
    },10000);
    $("#pauseButton").click(function(){
        // Pause timeouts and page
    });
});
</script>

EDIT 编辑
I have been notified that there is a possible duplicate answer, so I am now focusing on pausing animations and other page elements. 我被告知可能存在重复的答案,因此我现在集中在暂停动画和其他页面元素上。 That answer shows how to pause timeouts only. 该答案显示了如何仅暂停超时。

There are many ways to solve this issue. 有很多方法可以解决此问题。 Many of them are mentioned in this question as mentioned by @EmadZamout in the comments. 正如@EmadZamout在评论中提到的那样,其中许多问题已提及。

But, if you are looking for an easy and maybe an alternate way to solve this. 但是,如果您正在寻找一种简单的方法,也许是另一种解决方法。 Try this. 尝试这个。 Here I am using requestAnimationFrame to solve the issue 在这里我正在使用requestAnimationFrame解决问题

 let ran = Date.now(); // contains the last updated time let time = 0; // time in seconds let paused = false; // store the state const func = () => { if (!paused && Date.now() - ran > 1000) { time++; ran = Date.now(); console.log('now') } if (time === 8) return alert('This works!'); requestAnimationFrame(func); } func(); document.querySelector('button').addEventListener('click', () => paused = !paused); 
 <button>Change state</button> 

For stopping all the animations of the website, you need to manually stop each animation. 要停止网站的所有动画,您需要手动停止每个动画。

For stopping a jQuery animation, you can use .stop() helper. 要停止jQuery动画,可以使用.stop()帮助器。 An example: 一个例子:

 let paused = false; // state of the animation let dir = 'down'; // to store the direction of animation so that the next time continues in the correct direction let timeDown = 2000; // to animate properly after resuming let timeUp = 2000; // to animate properly after resuming // the initial calling of the animation (function() { slideDown(); })(); // function which resumes the animation function animate() { switch (dir) { case 'up': slideUp(); break; case 'down': slideDown(); break; } } // a function to animate in the uppward direction function slideUp() { dir = 'up'; // setting direction to up timeDown = 2000; // resetting the duration for slideDown function $('div').stop().animate({ left: 0 }, { duration: timeUp, complete: slideDown, // calling slideDown function on complete progress: function (animation, progress, ms) { timeUp = ms; // changing the duration so that it looks smooth when the animation is resumed } }); // actual animation } // a function to animate in the downward direction function slideDown() { dir = 'down'; // setting direction to down timeUp = 2000; // resetting the duration for slideDown function $('div').stop().animate({ left: 200 }, { duration: timeDown, complete: slideUp, // calling slideUp function on complete progress: function (animation, progress, ms) { timeDown = ms; // changing the duration so that it looks smooth when the animation is resumed } }); // actual animation } // button click event listener $('button').click(function() { if (paused) animate(); // to resume the animation else $('div').stop(); // to stop all the animations on the object paused = !paused; // toggling state }); 
 div { position: relative; width: 100px; height: 100px; background: dodgerblue; } 
 <button>Pause</button> <div></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

For bootstrap, I don't think you have any bootstrap animations which needed to be paused in this scenario which you have mentioned since bootstrap animations depend on user interactions. 对于自举,我认为您没有提到任何在这种情况下需要暂停的自举动画,因为自举动画取决于用户交互。 If you want to prevent user interaction, you can put an overlay over the website says "Paused". 如果要防止用户交互,可以在网站上放置一个“已暂停”的覆盖。 Or, if you don't want to do that you can use CSS property pointer-events: none to disable all the pointer events. 或者,如果您不想这样做,则可以使用CSS属性pointer-events: none禁用所有指针事件。

Now for CSS animations, you can set a property called animation-play-state to paused . 现在,对于CSS动画,您可以将名为animation-play-state的属性设置为paused

If you want to change the state of the animations to paused when the user is not on the page (As I understood for your updated questions) you can use the new visibilityState API for that. 如果要在用户不在页面上时将动画的状态更改为暂停(如我对您的更新问题的理解),则可以为此使用新的visibilityState There is an event visibilitychange which is fired when there a change in visibility. visibilitychange更改时会触发事件visibilitychange更改。

 document.addEventListener("visibilitychange", function() { console.log( document.visibilityState ); document.querySelector('div').innerHTML = document.visibilityState; }); 
 <div> Try opening a different tab or change the focus to another app </div> 

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

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