简体   繁体   English

运行后如何重复 JavaScript function?

[英]How can I repeat a JavaScript function after it's ran?

I'm using CSS/JavaSript to rotate a cube, problem is when I run the script it goes through all sides at once.我正在使用 CSS/JavaSript 来旋转立方体,问题是当我运行脚本时它会同时遍历所有面。 My solution was to use setTimeout() to allow each CSS animation to run.我的解决方案是使用 setTimeout() 来允许每个 CSS animation 运行。 This works great for the fist six, but after that it stops since there's only six iterations in the function. How can I get this function to repeat once it's done?这对前六个非常有用,但之后它停止了,因为 function 中只有六次迭代。完成后如何让这个 function 重复?

 function spinMe() { $('.cube').removeClass('show-top'); $('.cube').addClass('show-bottom'); setTimeout(function() { $('.cube').removeClass('show-bottom'); $('.cube').addClass('show-left'); }, 1000); setTimeout(function() { $('.cube').removeClass('show-left'); $('.cube').addClass('show-back'); }, 4000); setTimeout(function() { $('.cube').removeClass('show-back'); $('.cube').addClass('show-right'); }, 7000); setTimeout(function() { $('.cube').removeClass('show-right'); $('.cube').addClass('show-top'); }, 10000); } jQuery(document).ready(function() { spinMe(); });
 .cube.show-front { transform: translateZ(-100px) rotateY( 0deg); }.cube.show-right { transform: translateZ(-100px) rotateY( -90deg); }.cube.show-back { transform: translateZ(-100px) rotateY(-180deg); }.cube.show-left { transform: translateZ(-100px) rotateY( 90deg); }.cube.show-top { transform: translateZ(-100px) rotateX( -90deg); }.cube.show-bottom { transform: translateZ(-100px) rotateX( 90deg); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="scene"> <div class="cube"> <div class="cube__face cube__face--front">Front</div> <div class="cube__face cube__face--back">Back</div> <div class="cube__face cube__face--right">Right</div> <div class="cube__face cube__face--left">Left</div> <div class="cube__face cube__face--top">Top</div> <div class="cube__face cube__face--bottom">Bottom</div> </div> </div>

You could make the function recursive.您可以使 function 递归。 In that way it would infinite loop itself.这样它就会无限循环自己。 You must add a timeout before the recursion would take place, else you would stack a lot of setTimeout functions and I don't think the browser will like that.你必须在递归发生之前添加一个超时,否则你会堆积很多 setTimeout 函数,我认为浏览器不会喜欢那样。

For example you could do:例如你可以这样做:

 function spinMe() { $('.cube').removeClass('show-top'); $('.cube').addClass('show-bottom'); setTimeout(function() { $('.cube').removeClass('show-bottom'); $('.cube').addClass('show-left'); }, 1000); setTimeout(function() { $('.cube').removeClass('show-left'); $('.cube').addClass('show-back'); }, 4000); setTimeout(function() { $('.cube').removeClass('show-back'); $('.cube').addClass('show-right'); }, 7000); setTimeout(function() { $('.cube').removeClass('show-right'); $('.cube').addClass('show-top'); }, 10000); setTimeout(function() { spinMe(); }, 13000); } jQuery(document).ready(function() { spinMe(); });
 .cube.show-front { transform: translateZ(-100px) rotateY( 0deg); }.cube.show-right { transform: translateZ(-100px) rotateY( -90deg); }.cube.show-back { transform: translateZ(-100px) rotateY(-180deg); }.cube.show-left { transform: translateZ(-100px) rotateY( 90deg); }.cube.show-top { transform: translateZ(-100px) rotateX( -90deg); }.cube.show-bottom { transform: translateZ(-100px) rotateX( 90deg); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="scene"> <div class="cube"> <div class="cube__face cube__face--front">Front</div> <div class="cube__face cube__face--back">Back</div> <div class="cube__face cube__face--right">Right</div> <div class="cube__face cube__face--left">Left</div> <div class="cube__face cube__face--top">Top</div> <div class="cube__face cube__face--bottom">Bottom</div> </div> </div>

Just call spinMe() as recursive function from your last setTimeout只需从上一个setTimeout调用spinMe()作为递归 function

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

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