課程
/前端開發
/Node.js
/進擊Node.js基礎(二)
有人和我一樣 在第三個球去往300px的路上卡的走不動的嗎 ?遞歸加回調 直接把內存和cpu榨干了
2017-06-08
源自:進擊Node.js基礎(二) 1-2
正在回答
粗粗想了一下有可能是第一個方法 回調函數一直占用著上一層的函數 形成了閉包 而resolve()是再調用了整個函數 不是從函數內部去調用 所以不會對資源有大量損耗。 也可能我寫的第一段代碼本身有問題。。
同樣的邏輯 但是這段代碼流暢運行
var ball1 = document.querySelector('.ball1');
?var ball2 = document.querySelector('.ball2');
?var ball3 = document.querySelector('.ball3');
var Promise = window.Promise; ? ?//Bug window 寫成了Window
? ? ? ? function promiseAnimate(ball,distance){
? ? ? ? ? ? return new Promise(function(resolve,reject){
? ? ? ? ? ? ? ? function _animate(){ ? //此處在函數前加上_是私有函數書寫規范
? ? ? ? ? ? ? ? ? ? var marginLeft = parseInt(ball.style.marginLeft,10);
? ? ? ? ? ? ? ? ? ? setTimeout(function(){
? ? ? ? ? ? ? ? ? ? ? ? if ( marginLeft == distance) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? resolve();
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (marginLeft < distance) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft--;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ball.style.marginLeft = marginLeft+'px'; ? //Bug此處需要加px
? ? ? ? ? ? ? ? ? ? ? ? _animate();
? ? ? ? ? ? ? ? ? ? },13);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? _animate();
? ? ? ? ? ? })
? ? ? ? }
? ? ? ? promiseAnimate(ball1,100)
? ? ? ? ? ? .then(function(){
? ? ? ? ? ? ? ? return promiseAnimate(ball2,200)
? ? ? ? ? ? ? ? return promiseAnimate(ball3,300)
? ? ? ? ? ? ? ? return promiseAnimate(ball3,150)
? ? ? ? ? ? ? ? return promiseAnimate(ball2,150)
? ? ? ? ? ? ? ? return promiseAnimate(ball1,150)
這是第一段榨干內存的代碼
? ? ? ? var ball2 = document.querySelector('.ball2');
? ? ? ? var ball3 = document.querySelector('.ball3');
? ? ? ? // function animate(ball,distance,callback){
? ? ? ? // ? ? var marginLeft = parseInt(ball.style.marginLeft,10);
? ? ? ? // ? ? setTimeout(function(){
? ? ? ? // ? ? ? ? if ( marginLeft == distance) {
? ? ? ? // ? ? ? ? ? ? callback && callback();
? ? ? ? // ? ? ? ? } else {
? ? ? ? // ? ? ? ? ? ? if (marginLeft < distance) {
? ? ? ? // ? ? ? ? ? ? ? ? marginLeft++;
? ? ? ? // ? ? ? ? ? ? }else {
? ? ? ? // ? ? ? ? ? ? ? ? marginLeft--;
? ? ? ? // ? ? ? ? ? ? }
? ? ? ? // ? ? ? ? }
? ? ? ? // ? ? ? ? ball.style.marginLeft = marginLeft+'px'; ? //Bug此處需要加px
? ? ? ? // ? ? ? ? animate(ball,distance,callback);
? ? ? ? // ? ? },13);
? ? ? ? // }
? ? ? ? // animate(ball1,100,function(){
? ? ? ? // ? ? animate(ball2,200,function(){
? ? ? ? // ? ? ? ? animate(ball3,300,function(){
? ? ? ? // ? ? ? ? ? ? animate(ball3,150,function(){
? ? ? ? // ? ? ? ? ? ? ? ? animate(ball2,150,function(){
? ? ? ? // ? ? ? ? ? ? ? ? ? ? animate(ball1,150,function(){
? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? ? //回調結束
? ? ? ? // ? ? ? ? ? ? ? ? ? ? })
? ? ? ? // ? ? ? ? ? ? ? ? })
? ? ? ? // ? ? ? ? ? ? })
? ? ? ? // ? ? ? ? })
? ? ? ? // ? ? })
? ? ? ? // })
舉報
本教程帶你攻破 Nodejs,讓 JavaScript流暢運行在服務器端
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-06-08
粗粗想了一下有可能是第一個方法 回調函數一直占用著上一層的函數 形成了閉包 而resolve()是再調用了整個函數 不是從函數內部去調用 所以不會對資源有大量損耗。 也可能我寫的第一段代碼本身有問題。。
2017-06-08
同樣的邏輯 但是這段代碼流暢運行
var ball1 = document.querySelector('.ball1');
?var ball2 = document.querySelector('.ball2');
?var ball3 = document.querySelector('.ball3');
var Promise = window.Promise; ? ?//Bug window 寫成了Window
? ? ? ? function promiseAnimate(ball,distance){
? ? ? ? ? ? return new Promise(function(resolve,reject){
? ? ? ? ? ? ? ? function _animate(){ ? //此處在函數前加上_是私有函數書寫規范
? ? ? ? ? ? ? ? ? ? var marginLeft = parseInt(ball.style.marginLeft,10);
? ? ? ? ? ? ? ? ? ? setTimeout(function(){
? ? ? ? ? ? ? ? ? ? ? ? if ( marginLeft == distance) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? resolve();
? ? ? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (marginLeft < distance) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft++;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? marginLeft--;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ball.style.marginLeft = marginLeft+'px'; ? //Bug此處需要加px
? ? ? ? ? ? ? ? ? ? ? ? _animate();
? ? ? ? ? ? ? ? ? ? },13);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? _animate();
? ? ? ? ? ? })
? ? ? ? }
? ? ? ? promiseAnimate(ball1,100)
? ? ? ? ? ? .then(function(){
? ? ? ? ? ? ? ? return promiseAnimate(ball2,200)
? ? ? ? ? ? })
? ? ? ? ? ? .then(function(){
? ? ? ? ? ? ? ? return promiseAnimate(ball3,300)
? ? ? ? ? ? })
? ? ? ? ? ? .then(function(){
? ? ? ? ? ? ? ? return promiseAnimate(ball3,150)
? ? ? ? ? ? })
? ? ? ? ? ? .then(function(){
? ? ? ? ? ? ? ? return promiseAnimate(ball2,150)
? ? ? ? ? ? })
? ? ? ? ? ? .then(function(){
? ? ? ? ? ? ? ? return promiseAnimate(ball1,150)
? ? ? ? ? ? })
2017-06-08
這是第一段榨干內存的代碼
var ball1 = document.querySelector('.ball1');
? ? ? ? var ball2 = document.querySelector('.ball2');
? ? ? ? var ball3 = document.querySelector('.ball3');
? ? ? ? // function animate(ball,distance,callback){
? ? ? ? // ? ? var marginLeft = parseInt(ball.style.marginLeft,10);
? ? ? ? // ? ? setTimeout(function(){
? ? ? ? // ? ? ? ? if ( marginLeft == distance) {
? ? ? ? // ? ? ? ? ? ? callback && callback();
? ? ? ? // ? ? ? ? } else {
? ? ? ? // ? ? ? ? ? ? if (marginLeft < distance) {
? ? ? ? // ? ? ? ? ? ? ? ? marginLeft++;
? ? ? ? // ? ? ? ? ? ? }else {
? ? ? ? // ? ? ? ? ? ? ? ? marginLeft--;
? ? ? ? // ? ? ? ? ? ? }
? ? ? ? // ? ? ? ? }
? ? ? ? // ? ? ? ? ball.style.marginLeft = marginLeft+'px'; ? //Bug此處需要加px
? ? ? ? // ? ? ? ? animate(ball,distance,callback);
? ? ? ? // ? ? },13);
? ? ? ? // }
? ? ? ? // animate(ball1,100,function(){
? ? ? ? // ? ? animate(ball2,200,function(){
? ? ? ? // ? ? ? ? animate(ball3,300,function(){
? ? ? ? // ? ? ? ? ? ? animate(ball3,150,function(){
? ? ? ? // ? ? ? ? ? ? ? ? animate(ball2,150,function(){
? ? ? ? // ? ? ? ? ? ? ? ? ? ? animate(ball1,150,function(){
? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? ? //回調結束
? ? ? ? // ? ? ? ? ? ? ? ? ? ? })
? ? ? ? // ? ? ? ? ? ? ? ? })
? ? ? ? // ? ? ? ? ? ? })
? ? ? ? // ? ? ? ? })
? ? ? ? // ? ? })
? ? ? ? // })