简体   繁体   English

Javascript setInterval:第一个间隔突然

[英]Javascript setInterval: first interval is abrupt

When using JavaScript's setInterval method in the example below with jQuery effects, the initial display does not appear to engage the fadeIn method. 在下面的示例中使用JavaScript的setInterval方法和jQuery效果时, 初始显示似乎没有使用fadeIn方法。 "text" is displayed suddenly. 突然显示“文本”。

Subsequent iterations through the chain of effects starting with fadeIn() execute as expected. 以fadingIn()开始的效果链中的后续迭代将按预期执行。 It is just the first pass where the "text" is displayed suddenly without the fadeIn effect. 这只是突然显示“文本”而没有fadeIn效果的第一遍。

Any ideas as to why this occurs? 有什么想法为什么会这样? Thanks - a JavaScript beginner. 谢谢-JavaScript初学者。

setInterval(myFunction, 1000);
function myFunction() {
    $('#myId').fadeIn(1500).html("text").delay(2750).fadeOut(1500).delay(1000;
};

It could be Fading In without you noticing as the text is added after the fade In. 您可能没有注意到它,可能是“渐入”,因为在“渐入”之后添加了文本。 Switch it around, add the text before fading it in. Make sure it was not displayed beforehand (in CSS or however you need it). 切换它,在淡入之前添加文本。确保事先没有显示它(在CSS中,或者您需要它)。

$('#myId').html("text").fadeIn(1500).delay(2750).fadeOut(1500).delay(1000);

Assuming your text is initially blank and hidden, you are fading in an empty element and then setting the text to text only after the element is faded in. Reverse those steps: 假设您的文本最初是空白且隐藏的,那么您将淡入一个空元素,然后仅在淡入该元素后才将text设置为文本。请执行以下步骤:

$('#myId').html("text").fadeIn(1500).delay(2750).fadeOut(1500).delay(1000);

 setInterval(myFunction, 1000); function myFunction() { $('#myId').html("text").fadeIn(1500).delay(2750).fadeOut(1500).delay(1000); }; 
 #myId { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="myId"> </div> 

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

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