gpt4 book ai didi

javascript - 如何清除调用自身的函数内部的间隔javascript

转载 作者:行者123 更新时间:2023-12-03 05:12:03 26 4
gpt4 key购买 nike

我需要清除此示例中的函数间隔

$.fn.bounce = function(options) {

var settings = $.extend({
speed: 10
}, options);

return $(this).each(function() {

var $this = $(this),
$parent = $this.parent(),
height = $parent.height(),
width = $parent.width(),
top = Math.floor(Math.random() * (height / 2)) + height / 4,
left = Math.floor(Math.random() * (width / 2)) + width / 4,
vectorX = settings.speed * (Math.random() > 0.5 ? 1 : -1),
vectorY = settings.speed * (Math.random() > 0.5 ? 1 : -1);

// place initialy in a random location
$this.css({
'top': top,
'left': left
}).data('vector', {
'x': vectorX,
'y': vectorY
});

var move = function($e) {

var offset = $e.offset(),
width = $e.width(),
height = $e.height(),
vector = $e.data('vector'),
$parent = $e.parent();

if (offset.left <= 0 && vector.x < 0) {
vector.x = -1 * vector.x;
}
if ((offset.left + width) >= $parent.width()) {
vector.x = -1 * vector.x;
}
if (offset.top <= 0 && vector.y < 0) {
vector.y = -1 * vector.y;
}
if ((offset.top + height) >= $parent.height()) {
vector.y = -1 * vector.y;
}

$e.css({
'top': offset.top + vector.y + 'px',
'left': offset.left + vector.x + 'px'
}).data('vector', {
'x': vector.x,
'y': vector.y
});

setTimeout(function() {
move($e);
}, 50);

};

move($this);
});

};

$(function() {
$('#wrapper li').bounce({
'speed': 7
});
});

因此,每当我需要时,我都会启动动画循环,当我不需要时,我可以停止。因此,在上面的代码中,您可以看到 move($this); 在间隔内被调用,我需要停止或清除间隔,以便圆圈停止动画。当我再次需要时,我只需单击按钮即可再次启动动画。

最佳答案

我将您在 bounce 函数中的代码分为三个部分:

  1. 用于初始化,其中元素占据其起始位置。
  2. 另一个动画逻辑(添加了开始和停止)
  3. 最后一个是用于移动的(相同的函数 move 但在 each 内部定义它(不好,因为它会为每个元素重新定义),我在 each 之外定义它)。

代码包含大量注释。如果仍有不清楚的地方,请在下面发表评论。

$.fn.bounce = function(options) {

var settings = $.extend({
speed: 10
}, options);

// Keep a reference to this to use when we are inside bounded functions (where this is something different)
var that = this;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// LOGIC FOR INITIALIZATION ///////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// function init to initialize the elements.
function init(){
$(that).each(function() {

var $this = $(this),
$parent = $this.parent(),
height = $parent.height(),
width = $parent.width(),
top = Math.floor(Math.random() * (height / 2)) + height / 4,
left = Math.floor(Math.random() * (width / 2)) + width / 4,
vectorX = settings.speed * (Math.random() > 0.5 ? 1 : -1),
vectorY = settings.speed * (Math.random() > 0.5 ? 1 : -1);

// place initialy in a random location
$this.css({
'top': top,
'left': left
}).data('vector', {
'x': vectorX,
'y': vectorY
});
});
}
// call it right away (initialize) before starting anything else
init();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// LOGIC FOR ANIMATION ///////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// the boolean that will stop the animation
var keepGoing = false;

// If the selector for the start button is specified
if(settings.start){
// attach animate to its click event listener
$(settings.start).on("click", animate);
}
else // no button is provided then start automatically
animate();
// If the selector for the stop button is specified
if(settings.stop){
// attach stop to its click event listener
$(settings.stop).on("click", stop);
}

// the function that will start the animation
function animate(){
// If we are not already animating
if(!keepGoing){
keepGoing = true;
// call move on all the elements to start the magic.
// we use 'that' instead of 'this' here because 'this' is the button that have been clicked (see the event listener above=.
$(that).each(function() {
move($(this));
});
}
}
// the function that will stop the animation ...
function stop(){
// ... by simply set keepGoing to false
keepGoing = false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////// LOGIC FOR MOVEMENT ///////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// the move function responsible for moving the elements
function move($e) {

var offset = $e.offset(),
width = $e.width(),
height = $e.height(),
vector = $e.data('vector'),
$parent = $e.parent();

if (offset.left <= 0 && vector.x < 0) {
vector.x = -1 * vector.x;
}
if ((offset.left + width) >= $parent.width()) {
vector.x = -1 * vector.x;
}
if (offset.top <= 0 && vector.y < 0) {
vector.y = -1 * vector.y;
}
if ((offset.top + height) >= $parent.height()) {
vector.y = -1 * vector.y;
}

$e.css({
'top': offset.top + vector.y + 'px',
'left': offset.left + vector.x + 'px'
}).data('vector', {
'x': vector.x,
'y': vector.y
});

// if keep going, ... you know, keep going.
if(keepGoing){
setTimeout(function() {
move($e);
}, 50);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}

// the options can have the 'start' and 'stop' selector optionally.
$(function() {
$('#wrapper li').bounce({
'speed': 7,
'start': '#startAnimation', // selector of the element that when clicked the animation will start. If not provided the animation will start automatically
'stop' : '#stopAnimation' // selector of the element that when clicked the animation will stop (pause). If not provided the animation will go for ever
});
});
body, * {
padding: 0 !important; margin: 0: }

#wrapper {
width:500px;
height: 500px;
border:
1px solid red; }

li {
position: absolute;
width: 60px;
height: 60px;
-webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
background-color:#0FF;
line-height: 60px;
text-align:center;
cursor:pointer; }

button{
width: 100px;
height: 30px;
}
<script src='http://code.jquery.com/jquery-3.1.1.min.js'></script>
<ul id="wrapper">
<button id="startAnimation">Start</button>
<button id="stopAnimation">Stop</button>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>

关于javascript - 如何清除调用自身的函数内部的间隔javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41770555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com