在编写jQuery动画时,通过回调函数进行动画队列的编排,但当回调过多,往往会出现这样的代码:

 $(".box1").fadeIn(1000,function(){
$(".box2").fadeIn(1000,function(){
$(".box3").hide(1000,function(){
// todo xxx
})
})
})

那么是否可以将这种层次的回调以链式的写法完成呢?

如:f(fn1).f(fn2).f(fn3)... 表示fn1执行完成后fn2,fn2执行完成后执行fn3,...

下面逐步介绍实现方式:

1、确定结构:

为实现链式调用,需要f()的返回值一个包含其本身的对象

function f(n){
var obj = {};
obj.f = f;
return obj;
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdMAAAB7CAIAAACgpP3NAAAKHUlEQVR4nO3dy2oUTxuA8bqjugO9ABfyv4AKqA2CEpDsxo1EUKjFrLyE3rjsTWSWgeBCRAljyUcIhJAYRCULcxCPqW/Rp+rp0/SkpzJJnh9ZmJlJdePi4U1N90RYAIBf4qJPAACuHcoLAL5RXgDwraK8Z2d2vP2z5+NEVkXWRlbqnhcGgEunorx7n3/fffpp4/3JbCsabYWwQhUeVCp/VpvZFgaAK6J6t2Ft4/udJ7ubW7PEN46sljZKHzE6/7e1VjP2Arjeqst7fPr38Yu9e6vbuwc/uq5YDqsqzr+RKoQYAK6b2nfYtnZP/3v0cfn51tfDX9MuFlkhki+VT7xWFVvMhgOAa66+vDvHtx5s3l81Xw67vds2OfOWysvMC+Caqy7v0cmfh8/M7eV3O/vHXVds3W1gnxfANVdd3pev9m8svX774XCGFcthdYdcthoAoKK8O/tHN5fW19YPOi9mrEz3eSf2E5Kxl+t5AaCyvP/O7JvNbz0fJ3LecwOA6427hwHAN8oLAL5RXgDwjfICgG/ifwAAv5h5AcA3ygsAvlFeAPCN8gKAb5QXAHy7UuWNhkIEQgzneJ9yNBQiUDqUIhBqdGGnAeBSm2t5IxUIEVR9YMNYy7qnZmf0YNremVCKoLmP8ckLEUg9Th8bqSy4cYJrf3goxIAPZQNQrefyJuNeVtWR6imvkaqfMd3XTFfe+JVaD2rraUIpAqlDJQIhwzihE2WP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cxPnkhAqnH6WMjlQU3TnDtDw+FGPCBaACq9VzeZNzLqjpSPeU1UvUzpvua6cobv1LrQW09TShFIHWoRCBkGCd0ouyRyp8qHaCxywCuuV7LO1LJFFn4Rx8BcobNet3KWxfNmAnl5JlPnoPRg9rBlvICaNBneU2++2mScTJOsDsF22yrIc2W8xrnF/n8R5xvm3PW3lNbPJwIhBpluwrp12BlZeB+m7Q1mYLH+UoNea0INwCk+ixvNMx2RYvlTadgJ4vZwJgNqukj2SvdGbN95o2PWChjw5k6jS6XV5uqdJbK2zTzJn1nqxdAFQ8zr9TjZM6tKG82/6YjrS6NltYu5m4D+7wAZtTrPm+c0cl93nyAbZx5rQllYeZ105ZUr/l9tjmXt7h++7UNlBdAjZ6vbciv1kqvbZADObFhaq0tVMzZeJWhSeflmq3htn3e9vIWpuzq1dyd5UKg07HXNF7PaykvgEa+76Rwojblnmz3xed/J0XzAbiTAkCzK3UPGwBcCpQXAHyjvADgG+UFAN8oLwD4xl/ABADfmHkBwLf/Ay1e8Z9b/aTbAAAAAElFTkSuQmCC" alt="" />

这样就构造好了链式调用的基本结构。

尝试加入参数:

function f(n){
console.log(n);
var obj = {};
obj.f = function(m){return f(m);};
return obj;
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAd4AAAD3CAIAAAA8KKLeAAAQU0lEQVR4nO3dT4scxR/H8XpG9Qx+PgAP4gOogNogKAHJbbxIBGXrMCchT2D24LEIROYYEjyIKMvaEpbAsmQNorIHs4n4d+t36H/V/2d6ajbfmX2/2MPMbE9N9R4+/d3vVHcrDwAQRr3uCQAAmohmABCHaAYAcTqi+erKHz/9M/LnOG9cx8tW+66XAeBG64jmZz///e6nPz3+/uW0EVPrlfLK1F404VPnbdrzKwBAX0PjwePf3/nk7OhkSjpnURuWw6mtHlvtlapFcyOpAQDd0Xz56t+Pv3j23t2nZ8//WHdEa5uvNOri1NazOPWm9RYAuMl6vwY8OXv19kc/3v785NeLv1YdzHml8p+qs9xK3mY009MAgL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hDNAOAOEQzAIhDNAOAOEQzAIizV9Hs5kolSs338OZXbq5UYuxCq0SZ5cA2+7n7wE2z1Wh2JlEq6bpN4LHVfb+aLrWzVYMpXWiVDAdZNnmlEm2Po86wP1t7LU35riyjeyc9V2rGVf+AnRc5mvPCrYzdpYmUv86MJ5ozq9aM2ZbWznpjLl1olWi7MCpRejE16+LsfuOQ40z/lIaDG8CuiBrNS5PXobUHMZIiKBv7rRfNw4GbLvTmM48TlM19T+2stzQmmoH9EDOa06oTmuYFaZbRYR3ty25GkS/BNsH/7NVbgqfDuTMeuL7+cSpRZlk2Loqf2Z07s/CpdVn3Y+6KZq61M6VmWtc6Hnmzotiv+rBzF2xQ7kLwlsS4Y6sTpWc6GKT8q9aaKgP5G+WIAuC1ixnNbt7IqSKaizo6yM2y9CtL3eKVcsuwWhyvmrNPXLEvHIZ4O5pt2sy4sqVQzDY7uhT7ZZbVYSlM0naGhsNWR7JitPKV8I2taB6qmvMDD+1mYMddQ9Ws7XGePh3RXFbQRfFoW0Wi96+9odEZzdV+NcK0+pjBaA5+m/813EJ3HBia+06vGdh/UXvN9VqyViyPV80+zYKp2jLIoDyehr8M3HI0F3PrjOZGCVxbTRE03Mer5o6avbZf4ys0iGZg90VeoVEtSitWaLSbp977WtwEzV+9SJsd2Gzzqrge6TWPR3OtTu8eLexuZwle73eXrQ9jqvZx0BiptYnbveayq97Ra1aJ0nOjGwv7Wm2T3j8B0Qzshes+5STIuIjrheuD7+M5F6tk7h7vPnDT7NXZgACwH4hmABCHaAYAcYhmABCHewMCgDhUzQAgDtEMAOL8H10SVsLJom5hAAAAAElFTkSuQmCC" alt="" />

与我们预想的结果相同,但如结果所见,f调用()会立即将参数代入执行。现在增加一个需求,希望f().f().f()...仅表示一个序列,真正的执行需要我下达其他命令后才发生。于是我们需要在f()的obj中加入一个函数run(),此函数用来下达执行指令,修改代码如下:

 function f(n){
var obj = {};
obj.f = function(m){return f(m);};
obj.run = function(){
console.log(n)
}
return obj;
}

aaarticlea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qp4LL28mhuqPSlXMfyp+NDJXDE6Wh+xlaP0jCHazMDnLuUqJc3CFKf3fypnWsn8qdUEKs4c6WU7aJAtaMOny4QiYx2ULGe1qW62iZY6/4gFXcp0fhEB6WC/KkNm8ifEhWMV8wrZfuBidpPOiWBnoJVuJ7n+T1hyvhcZxj8EEdLvy3FAotmSloZz/ODtTjA70fdNaxfy35nAgDMAnoKAAB5gJ4CAEAeoKcAAJAH/P8oAADIA/wpAADkAXoKAAB5+D/RhgoVIjbw2QAAAABJRU5ErkJggg==" alt="" />

可以看到,在我们原先的代码并没有输出结果,而当我们执行run()后会怎样呢?

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcEAAAArCAIAAACPcaTvAAADgElEQVR4nO3dy2oUQRTG8XqjegN9ABfiA1RAXQhKwLUbiaCk1kKeIAuXRSCuQ4ILESVoyxACISQGUcnCXMRrjovumelL9fT0yXTMOP8fWXR6ZqrPzOLjVHUlYwQAoGX+dQEAMMXIUADQI0MBQI8MBQC9SIaencnb7e8Tvk4QFyKnvZXY6bF4J0nsQtZrR+ygyEbpu+j0EgC6E8nQvY8/bz36sPHmRDdi4sUYMa5w0uV/DeKTmodqOCPGFF6V+P6viVhTuOLwIW2R3ooxYuwwo8cpUiFfakeXANCp+Fx+dePrzYe7m1uaGE2zIN9YJX54nMZTIeNCQ+RlQVNsMH3/OLhs8OCG4/imVnRUkYk4n50ZtqVNReoU6uzmEgA6Fc/Q49PfD57u3V7Y3j341nbEan6VOqxyn9jPrDr5CK6O4G12nD85CFZ1keXCmopUaPs5ALiEau8pbe2e3rj/fv7J1ufDH+MOFsSY7GfYwVWioTrXHjGNzabVxZl1NCLzy6OjpvPjFVkdZESRzoixErwYI9ZLcFnL7K1Yn602eN/8LpjOA1OnPkN3jq/d3byzkHw6bHd/qdziNWZo+z60mj6DbrTuCW2L9LaQd41FZnFZGj+3LJA+mi+sXCR9KDCF4hl6dPLr3uPk+vzrnf3jtiO2nsuPsR46Yi4vlQCN1tC2SBGRROzgZFORpU0C1QwNUjiQ9p8DgEsonqHPnu9fmXvx6t2hYsRqPJUarvHnyIPnl5vKXMuWv0HvQ2T81kXmbl65fivaWGQpQ9PnD9rPaIaWGk8m8sA0imTozv7R1bm11bWD1oMNthmZ2pW+wfpmuhZZ3hcZyq8NLnu+rVkQcGa4vlm9fS+VWXarItNLNBSZGzB/p8sYsU6sER/Epvuokv5B5V2wPxSYUpEM/XMmLze/TPg6NdvXzyO+x/48Oiiy0eTfBYALxN96AoAeGQoAemQoAOiRoQCgR4YCgJ7pAQC06EMBQI8MBQA9MhQA9MhQANAjQwFAjwwFAL3JZmjP20VjFo1d4f9oAJgFE83QZMX5nogkfuni/wESAFy8buby/TAFgP9bJxma+GW+1gLALJh4hva8ZT0UwKzoai5v3XonIwPAZTLRDA3LNlsGXXe0ogBmQDd7m8wS66EAZgF77AFAjwwFAD0yFAD0yFAA0CNDAUCP71MCAD36UADQI0MBQO8vRNlhdXyeQh0AAAAASUVORK5CYII=" alt="" />

可以看到仅输出了第三次结果,这是因为run()并没有保存前两次f()参数的值,我们期望的将三次f()一并执行,实际上仅仅执行了第三次。那么如何解决这个问题呢,换句话说:如何在run()中保存所有之前获得的f()的参数呢?

2、保存参数:

    我们知道,函数通过参数获得函数外部变量的值,这里我们就通过这种方式保存f()的所有参数,代码修改如下:

 function f(n,fn){
var obj = {};
obj.arr = fn===undefined?[]:[fn];
obj.arr.push(n);
obj.f = function(m){return f(m,n);};
obj.run = function(){
$.each(obj.arr,function(i,fn){
fn();
});
}
return obj;
}

f()中添加obj.arr数组,用来保存所有传入的参数,而run()统一执行所有传入的参数,f()的第二个参数为上一次传入的参数,这样上一次执行所需的参数就被保存了下来。我们将传入的参数换成函数,用如下代码测试:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZMAAABDCAIAAADWN3+tAAAEp0lEQVR4nO3d7XHjIBCAYTpSRfQTGrgKUgCTElIDMyln74dkhAAJJDk2e/c+vzy2LD4WLwg5jvkBAG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EuGvmvN64iKkpm0kd108t2nFqSl+BZghlcObJNb25DgAW767/Xma/pqYctJdzVwK4GfweHQBkIF8DgAzkawCQgXwNADKQrwFABnUCAJCA9jUAyEC+BgAZyNcAIAP5GgBkIF8DgAzkawCQIWe+tnql1EqpA6b6BIDs8uXr8qE2J9WLQh9lWy0AwDmXM1/bQ1Wn6SNNvgaA3GYYv7aHzNEMANnlztdNKxsAkFPWfE2yBoDZZMzXzbC1NQ+5QwQA8sqWr0tzoNSq/qOVDQC58XsZAJCBfA0AMpCvAUAG8jUAyEC+BgAZeH4jAMhA+xoAZCBfA4AM5GsAkIF8DQAykK8BQAbyNQDIkDFfn5hipdRKFUzOBwD55X9+Y2kOeL4MAGQ3w3iIf/AuACCf/Pm6NIeGAREAyC1vvj4xBePXADCLWcZDCp4vAwC55cvX9rCoh62PNE1sAMhthvv51AHj1wCQHb+XAQAZyNcAIAP5GgBkIF8DgAzkawCQgXwNADLwvF0AkIH2NQDIQL4GABnI1wAgw/9uU1ZNxWOvZwAAAABJRU5ErkJggg==" alt="" />

再次向后追加,希望输出1 2 3,但实际只输出后两次结果,原因在于f()的第二个参数仅保存了上一次传入的参数,而不是前几次的

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcAAAADmCAIAAADNxcK7AAAVkklEQVR4nO3dPa/sxn3H8Xkhafke3EtIMY2BdE5cWykHdpNK2VaAgW0cAVSxhgLbjQUtnEZCACHFUoVSSYUDHEgqLFxLV9DV1RMGONiD3U3BpyE5wx3O8mFJfj84xb08u0Nyzz2/O8MZ/ik0ACCImPoAAGCuCFAACESAAkAgAhQAAhGgABCIAB3ERmxu+Zr68AF4IUAHcUsIEqDAXBCggyBAgTUgQAdBgAJrQIAOggAF1oAAHQQBCqyBf4A+qO1GbHax14uPUlyEuAhxidRj6LF57ujkd0j+YikykUpq36lvcWiEoLtNU6KkSqwBmqhICNn3mQK4iW+AxruNPOh45xeg8anv3/WjlMd+W3SKpePgExX5nlY9BJ1tWnZhDVDf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1Q/quhkO5hiOP+ookuaWLHM+qexLK6BWtv0DtBqOmb5Wa25F8srQ3hzwA9gjnwr0jvK1I8jnefJvqKi41mZMirDNFFn1ysjdVRROrlkazO7LHCKsymmbBrKzpgcSlMwXcakGlNL5XFVApQKp8DssZB+NNUhfXOSCcDcEKBDK3uqXAEFFoYABYBABCgABCJAASAQAQoAgQhQAAhEgAJAIAIUAAIRoAAQqGtF+o3YbNVDwI64cxHA0nSrSK+11g/7aLsPCkIKtgNYlG7VmLTWWj+oHQEKAAEBethlXdHOCFAAi9IxQA87ETh+1wQogIXpEKDJfntDeqaNSkoSAVgM3wCNdxuxCxy65+iBAlgUz4r0B7m5vSI9AQpgUcZcSE+AAliU0QKUhfQAloZbOQEgEAEKAIEIUAAIRIACQCACFAACEaAAEIgABYBABCgABCJAASAQAQoAgf4ftn5hiGOmgccAAAAASUVORK5CYII=" alt="" />

f()的第二个参数为数组,用来保存前几次已传入的参数,修改代码如下:

function f(n,ar){
var obj = {};
obj.arr = ar===undefined?[]:ar;
obj.arr.push(n);
obj.f = function(m){return f(m,obj.arr);};
obj.run = function(){
$.each(obj.arr,function(i,fn){
fn();
});
}
return obj;
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj0AAABWCAIAAACrX7DmAAAGgklEQVR4nO3dzYocxx0A8H6jeYPkAXwweYAyxD4YEgw+5xIciNk+B/YJ9pBjI3DORiaHEBJE0mEQBiGsGOMEHWLZIR+OKoeZ6a/q+dzq1dby+6HDMFtTXV3/mvlXVfeMqggA5ajedAMA4AzyFgAlkbcAKIm8BUBJ+rz1+nX80+f/ylx9E0OTucrbq0NsY6xXcbZpbR2rVWzvulH5bU5zqomrelxsNX4mKXAG4X5zhHtDuEfFMob7qM14WPQQO33e+uKr/7z7i79+9sdvL6uorWNVxSqMngzhSIFbaS4Zgm0d691rwlxjmnrBYV2vYlXNv6PmNbGqLhkHw9OMMdb13j9N/jpbYLZ+4T7qDYS7jatq1PPCPar1gYW7iVUVq6qf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IeTtdnRNu2ouW9XjEb20a+ACPc9Dnd3xO1LhHvfuT+IcPcdtbtSeMtwdw3oe/VoNHfC/jZk1+et/72Ov3vy98zV3+NvJu7TFvXNxHTod068dWr6OXUbwr0w4T5KuEfFMob7qDscD37nCYCSyFsAlETeAqAk8hYAJZG3AChJtQaAclhvAVASeQuAkshbAJRE3gKgJPIWACWRtwAoSb681Txa+rfrASBL3lrXq6uqupa3AFhatvVWW9/IWwAsTd4CoCTyFgAlkbcAKIm8BUBJMt5PeFVVV6HJUR8A7OF7xwCURN4CoCTyFgAlkbcAKIm8BUBJqjUAlMN6C4CSyFsAlETeAqAk8hYAJZG3ACiJvAVASfLkrSZsflf32k/CA7CoHHmrfRTq9ebBKjzOUCEA7JEjbzU31TZdPQ7yFgBLynp9q7nx/28BsKh8eatfdQHAUjLlLUkLgDuRJW/1l7Wa+pE7CgFYToa81dbXVXW1/WfVBcCSfO8YgJLIWwCURN4CoCTyFgAlkbcAKIm8BUBJqjUAlMN6C4CSyFsAlETeAqAk8hYAJZG3ACiJvAVASbLkrXW9uqqqq2rlPzEBYFk58lb7KNTrGGNbX4cmQ30AsE/WfcJdAgOAheTMW219U9soBGBJufLWul65vgXA4jLvE67C45wVAsBYjrzV3Ky2l7UeB0suAJaU9T746tr1LQAW5XvHAJRE3gKgJPIWACWRtwAoibwFQEmqNQCUw3oLgJLIWwCURN4CoCTyFgAl+T+i+GGO5qrNOgAAAABJRU5ErkJggg==" alt="" />

再次执行之前的代码,已符合预期。

3、执行回调

f(
function () {
$(".box1").fadeIn(1000);
}
).f(
function () {
$(".box2").fadeIn(1000);
}
).run();

执行上述代码,我们希望看到的是box1用1秒淡入,这个动画执行完后,box2开始执行1秒淡入,但实际上,这两个动画几乎是同时执行,所以接下来的关键是如何执行回调。修改测试代码如下:

f(
function (cb1) {
$(".box1").fadeIn(1000,cb1);
}
).f(
function (cb2) {
$(".box2").fadeIn(1000,cb2);
}
).run();

我们看到,cb1和cb2分别为box1和box2执行动画的回调函数,也就是说,我们希望达到这样的效果:在cb1中,执行box2的动画,在cb2中执行后面的动画(如果有),即:需要在cb中下达执行下一个f()中函数的指令。而run()仅仅是控制f()当前携带函数的执行,修改代码如下:

function f(n,ar){
var obj = {};
obj.arr = ar===undefined?[]:ar;
obj.arr.push(n);
obj.f = function(m){return f(m,obj.arr);};
obj.run = function(){
var fn = obj.arr.shift();
if(typeof fn === "function") fn(callback);
}
function callback(){
obj.run();
}
return obj;
}
f(
function (cb) {
$(".box1").fadeIn(1000,cb);
}
).f(
function (cb) {
$(".box2").fadeIn(1000,cb);
}
).run();

再次执行以上代码进行测试,实现box1执行完成后执行box2,目标实现

用同步的方式执行jQuery异步动画的更多相关文章

  1. js文件引用方式及其同步执行与异步执行

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp74   任何以appendChild(scriptNode) 的方式引入 ...

  2. 深入理解 JS 引擎执行机制(同步执行、异步执行以及同步中的异步执行)

    首先明确两点: 1.JS 执行机制是单线程. 2.JS的Event loop是JS的执行机制,深入了解Event loop,就等于深入了解JS引擎的执行. 单线程执行带来什么问题? 在JS执行中都是单 ...

  3. linux 异步信号的同步处理方式

    关于代码的可重入性,设计开发人员一般只考虑到线程安全,异步信号处理函数的安全却往往被忽略.本文首先介绍如何编写安全的异步信号处理函数:然后举例说明在多线程应用中如何构建模型让异步信号在指定的线程中以同 ...

  4. Jquery 自定义动画同步进行如何实现?

    需求描述:我需要对不懂的两个HTML对象进行操作,同时开始动画,同时结束动画. 遇到问题:如果先后对连个对象进行 animate动画,那么第一个会先运行,等第一个运行完了运行第二个,这样就不同步了. ...

  5. [每日一题]面试官问:Async/Await 如何通过同步的方式实现异步?

    关注「松宝写代码」,精选好文,每日一题 ​时间永远是自己的 每分每秒也都是为自己的将来铺垫和增值 作者:saucxs | songEagle 一.前言 2020.12.23 日刚立的 flag,每日一 ...

  6. MVC Ajax Helper或jQuery异步方式加载部分视图

    Model: namespace MvcApplication1.Models { public class Team { public string Preletter { get; set; } ...

  7. jQuery异步获取json数据的2种方式

    jQuery异步获取json数据有2种方式,一个是$.getJSON方法,一个是$.ajax方法.本篇体验使用这2种方式异步获取json数据,然后追加到页面. 在根目录下创建data.json文件: ...

  8. jQuery判断动画是否执行完成

    JS $(function() { $("#myDiv").bind("click", function() { if ($(this).css("t ...

  9. jQuery的动画处理总结

    最近一年多一直在做前端的东西,由于老板在追求一些年轻动感的效果,让页面元素不能仅仅是简单的隐藏显示,所以经常会使用一些动画效果,发现jQuery的动画真心好用啊,把常用的几个总结一下,希望不再每次使用 ...

随机推荐

  1. 简明python教程三-----函数

    函数通过def关键字定义.def关键字后跟一个函数的表标识符名称,然后跟一对圆括号. 圆括号之中可以包括一些变量名,该行以冒号结尾.接下来是一块语句,它们是函数体. def sayHello(): p ...

  2. C++匿名命名空间

    当定义一个命名空间时,可以忽略这个命名空间的名称:      namespce {          char c;          int i;          double d;      } ...

  3. apache错误 Unable to open process" with PID 4!

    今天打开Apache的时候报错: 22:15:30  [Apache] Problem detected! 22:15:30  [Apache] Port 80 in use by "Una ...

  4. XMPP环境搭建 (mac环境下 安装自己独立的mysql与服务器(openfire),实现即时聊天功能)

    1简单概览 [一]XMPP简介 http://xmpp.org 即时通讯技术 IM - Instant Messaging ⽀支持⽤用户在线实时交谈.交谈双⽅方都需要⼀一个聊天窗⼝口,其中⼀一个⽤用户 ...

  5. delphi webbrowser post自动登录

    delphi webbrowser post自动登录     var  EncodedDataString: WideString;  PostData: OleVariant;  Headers: ...

  6. Xcode 错误问题以及解决方法(后期遇到还会添加)

    1,/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhon ...

  7. 微信小程序学习笔记(4)--------框架之逻辑层

    逻辑层 逻辑层(App Service):小程序框架的逻辑层是由JavaScript编写的,逻辑层将数据进行处理后发送给视图层,同时接受视图层的事件反馈. App进行程序注册,Page进行页面注册 g ...

  8. 菩提树下的杨过.Net 的《hadoop 2.6全分布安装》补充版

    对菩提树下的杨过.Net的这篇博客<hadoop 2.6全分布安装>,我真是佩服的五体投地,我第一次见过教程能写的这么言简意赅,但是又能比较准确表述每一步做法的,这篇博客主要就是在他的基础 ...

  9. Servlet+MyBatis项目转Spring Cloud微服务,多数据源配置修改建议

    一.项目需求 在开发过程中,由于技术的不断迭代,为了提高开发效率,需要对原有项目的架构做出相应的调整. 二.存在的问题 为了不影响项目进度,架构调整初期只是把项目做了简单的maven管理,引入spri ...

  10. Linux网络配置脚本

    #!/bin/bash ip=$1 if [ -f "/etc/sysconfig/network-scripts/ifcfg-bond1" ] then break else # ...