admin管理员组

文章数量:1323335

Supose I have a function that calls the same function twice, with differents parameter each time, some like this:

function one(){
     two(a,b);
     two(c,d);  
}

When I call function one, only the first function two is executed, but not the second one. Is there a way to do this in Javascript only? (not Jquery)

Here's the code in cuestion (is a little text-based RPG)

window.onload = init;

function init(){

    document.onmousedown = function disableselect(e) {return false;};

 /*ELEMENTS*/

    var monsterPicture = document.createElement('div');
    monsterPicture.setAttribute('class', 'monsterPicture');
    monsterPicture.style.position = 'absolute';
    monsterPicture.style.top = '0';
    monsterPicture.style.right = '0';
    monsterPicture.style.bottom = '0';
    monsterPicture.style.left = '0';
    monsterPicture.style.height = '350px';
    monsterPicture.style.width = '350px';
    monsterPicture.style.margin = 'auto';
    monsterPicture.style.backgroundColor = 'grey';
    document.body.appendChild(monsterPicture);

    var textInfo = document.createElement('textarea');
    textInfo.setAttribute('class', 'textInfo');
    textInfo.style.position = 'absolute';
    textInfo.style.top = '0';
    textInfo.style.bottom = '0';
    textInfo.style.right = '0';
    textInfo.style.height = '350px';
    textInfo.style.width = '250px';
    textInfo.style.margin = 'auto 50px auto auto';
    textInfo.style.backgroundColor = 'white';
    textInfo.style.overflowY = 'hidden';
    textInfo.style.resize = 'none';
    textInfo.readOnly = 'true';
    textInfo.disabled = 'true';
    textInfo.style.cursor = "default";
    document.body.appendChild(textInfo);

    var statsArea = document.createElement('div');
    statsArea.setAttribute('class', 'statsArea');
    statsArea.style.position = 'absolute';
    statsArea.style.top = '0';
    statsArea.style.top = '0';
    statsArea.style.bottom = '0';
    statsArea.style.right = '0';
    statsArea.style.height = '350px';
    statsArea.style.width = '200px';
    statsArea.style.margin = 'auto 700px auto auto';
    document.body.appendChild(statsArea);


    var heroInfo =  document.createElement('textarea');
    heroInfo.setAttribute('class', 'heroInfo');
    heroInfo.style.height = '160px';
    heroInfo.style.width = '200px';
    heroInfo.style.marginTop = '10px';
    heroInfo.style.backgroundColor = 'white';
    heroInfo.style.overflowY = 'hidden';
    heroInfo.style.resize = 'none';
    heroInfo.readOnly = 'true';
    heroInfo.disabled = 'true';
    heroInfo.style.cursor = "default";
    document.body.appendChild(heroInfo);

    var monsterInfo =  document.createElement('textarea');
    monsterInfo.setAttribute('class', 'monsterInfo');
    monsterInfo.style.height = '160px';
    monsterInfo.style.width = '200px';
    monsterInfo.style.backgroundColor = 'white';
    monsterInfo.style.overflowY = 'hidden';
    monsterInfo.style.resize = 'none';
    monsterInfo.readOnly = 'true';
    monsterInfo.disabled = 'true';
    monsterInfo.style.cursor = "default";
    document.body.appendChild(monsterInfo);

    statsArea.appendChild(monsterInfo);
    statsArea.appendChild(heroInfo);




    /*CONSTRUCTOR FUNCTIONS*/

    function character (name, hitpoints, armorclass, attackbonus, weapondamage) {
    this.name = name;
    this.hitPoints = hitpoints;
    this.armorClass = armorclass;
    this.attackBonus = attackbonus;
    this.weaponDamage = weapondamage;
    this.stats = function(){
    return  this.name + "\n" +
    "Hit Points: " + this.hitPoints + "\n" +
            "Armor Class: " + this.armorClass + "\n" +
            "Attack Bonus: " + this.attackBonus + "\n" +
            "Weapon Damage: " + this.weaponDamage;
    };
    this.alive = true;
    this.reset = function (){
        this.hitPoints = hitpoints;
        this.armorClass = armorclass;
        this.attackBonus = attackbonus;
        this.weaponDamage = weapondamage;
      };
}

var Arquer = new character("Arquer", 15, 6, 5, 8);  

function selectMonster () {
    var werewolf = new character("Werewolf", 15, 4, 4, 3);
    var goblin = new character("Goblin", 15, 4, 4, 3);
    switch(Math.floor(Math.random()*2)+1){
      case 1: return werewolf;
      case 2: return goblin;
    }
 }


var buttonAttack= document.createElement('input');
buttonAttack.setAttribute('type','button');
buttonAttack.setAttribute('value','Attack');
document.body.appendChild(buttonAttack);

var current_monster = selectMonster();

heroInfo.value = Arquer.stats() + "\n" + "Alive: " + Arquer.alive;
monsterInfo.value = current_monster.stats() + "\n" + "Alive: " + current_monster.alive; 

buttonAttack.onclick = function(){
if (current_monster.hitPoints <= 0){current_monster = selectMonster();monsterInfo.value = current_monster.stats() + "\n" + "Alive: " + current_monster.alive;}  
else{battle(Arquer, current_monster);}
};


function battle (hero, monster){

  if(hero.alive===true && monster.alive===true){
    var heroIniciative = Math.floor(Math.random()*20)+1;
    var monsterIniciative = Math.floor(Math.random()*20)+1;
    var attacker;
    var defender;
    var attackerInfo;
    var defenderInfo;
    /*INICIATIVE ROLL*/
    if (heroIniciative >= monsterIniciative){
        attacker = hero;
        defender = monster;
        attackerInfo = heroInfo;
        defenderInfo = monsterInfo;
        textInfo.value += attacker.name + " attacks first!: " + heroIniciative + " vs " + monsterIniciative + "\n";
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    else {
    attacker = monster;
    defender = hero;
    attackerInfo = monsterInfo;
    defenderInfo = heroInfo;
        textInfo.value += attacker.name + " attacks first!: " + monsterIniciative + " vs " + heroIniciative + "\n",
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    check_defeat(attacker, defender, attackerInfo, defenderInfo);
  }
  else {reset (hero, monster);
}
}


function attack (attacker, defender, attackerInfo, defenderInfo){
    var d20 = Math.floor(Math.random()*20)+1;
    var d_wp = Math.floor(Math.random()*attacker.weaponDamage)+1;
    /*ROUND ONE*/
    if (d20+attacker.attackBonus>defender.armorClass){
        textInfo.value += attacker.name +" d20+" + attacker.attackBonus+": " + (d20+attacker.attackBonus)+ " vs AC " + defender.armorClass + "\n" + attacker.name +" hits! d" + attacker.weaponDamage + ": " + d_wp  + "\n";
        textInfo.scrollTop = textInfo.scrollHeight;
        defender.hitPoints = defender.hitPoints - d_wp;
        defenderInfo.value = defender.stats();
        defenderInfo.append("\n" + "Alive: " + defender.alive);
        }
    else {
        textInfo.value += attacker.name + " misses! d20+" + attacker.attackBonus+": " + (d20+attacker.attackBonus)+ " vs AC " + defender.armorClass;
        textInfo.scrollTop = textInfo.scrollHeight;
        defenderInfo.value = defender.stats();
        defenderInfo.append("\n" + "Alive: " + defender.alive);
   }}


function check_defeat (attacker, defender, attackerInfo, defenderInfo) {
    if (attacker.hitPoints <= 0){
        attacker.hitPoints = 0;
        attacker.alive = false,
        attackerInfo.value = attacker.stats();
        attackerInfo.append("\n" + "Alive: " + attacker.alive);
        textInfo.value += "\n" +defender.name + " killed " + attacker.name + "!";
        textInfo.scrollTop = textInfo.scrollHeight;
     } 
    if (defender.hitPoints <= 0){
        defender.hitPoints = 0;
        defender.alive = false,
        defenderInfo.value = defender.stats();
        defenderInfo.append("\n" + "Alive: " + defender.alive);  
        textInfo.value += "\n" + attacker.name + " killed " + defender.name + "!";
     }
}

function reset (hero, monster) {
    if (hero.alive===false){
        hero.reset();
        hero.alive = true;
        heroInfo.value = hero.stats();
        heroInfo.append("\n" + "Alive: " + hero.alive);  
    }
    if (monster.alive===false){
        monster.reset();
        monster.alive = true;
        monsterInfo.value = monster.stats(); 
        monsterInfo.append("\n" + "Alive: " + monster.alive);  
    }
}  
}

(For some reason it doesn't work in jsfiddle). The problem is in the function battle.

function battle (hero, monster){

  if(hero.alive===true && monster.alive===true){
    var heroIniciative = Math.floor(Math.random()*20)+1;
    var monsterIniciative = Math.floor(Math.random()*20)+1;
    var attacker;
    var defender;
    var attackerInfo;
    var defenderInfo;
    /*INICIATIVE ROLL*/
    if (heroIniciative >= monsterIniciative){
        attacker = hero;
        defender = monster;
        attackerInfo = heroInfo;
        defenderInfo = monsterInfo;
        textInfo.value += attacker.name + " attacks first!: " + heroIniciative + " vs " + monsterIniciative + "\n";
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    else {
    attacker = monster;
    defender = hero;
    attackerInfo = monsterInfo;
    defenderInfo = heroInfo;
        textInfo.value += attacker.name + " attacks first!: " + monsterIniciative + " vs " + heroIniciative + "\n",
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    check_defeat(attacker, defender, attackerInfo, defenderInfo);
  }
  else {reset (hero, monster);
}
}

When I call it, it just execute the first function attack, but not the second one.

buttonAttack.onclick = function(){
if (current_monster.hitPoints <= 0){current_monster = selectMonster();monsterInfo.value = current_monster.stats() + "\n" + "Alive: " + current_monster.alive;}  
else{battle(Arquer, current_monster);}
};

Supose I have a function that calls the same function twice, with differents parameter each time, some like this:

function one(){
     two(a,b);
     two(c,d);  
}

When I call function one, only the first function two is executed, but not the second one. Is there a way to do this in Javascript only? (not Jquery)

Here's the code in cuestion (is a little text-based RPG)

window.onload = init;

function init(){

    document.onmousedown = function disableselect(e) {return false;};

 /*ELEMENTS*/

    var monsterPicture = document.createElement('div');
    monsterPicture.setAttribute('class', 'monsterPicture');
    monsterPicture.style.position = 'absolute';
    monsterPicture.style.top = '0';
    monsterPicture.style.right = '0';
    monsterPicture.style.bottom = '0';
    monsterPicture.style.left = '0';
    monsterPicture.style.height = '350px';
    monsterPicture.style.width = '350px';
    monsterPicture.style.margin = 'auto';
    monsterPicture.style.backgroundColor = 'grey';
    document.body.appendChild(monsterPicture);

    var textInfo = document.createElement('textarea');
    textInfo.setAttribute('class', 'textInfo');
    textInfo.style.position = 'absolute';
    textInfo.style.top = '0';
    textInfo.style.bottom = '0';
    textInfo.style.right = '0';
    textInfo.style.height = '350px';
    textInfo.style.width = '250px';
    textInfo.style.margin = 'auto 50px auto auto';
    textInfo.style.backgroundColor = 'white';
    textInfo.style.overflowY = 'hidden';
    textInfo.style.resize = 'none';
    textInfo.readOnly = 'true';
    textInfo.disabled = 'true';
    textInfo.style.cursor = "default";
    document.body.appendChild(textInfo);

    var statsArea = document.createElement('div');
    statsArea.setAttribute('class', 'statsArea');
    statsArea.style.position = 'absolute';
    statsArea.style.top = '0';
    statsArea.style.top = '0';
    statsArea.style.bottom = '0';
    statsArea.style.right = '0';
    statsArea.style.height = '350px';
    statsArea.style.width = '200px';
    statsArea.style.margin = 'auto 700px auto auto';
    document.body.appendChild(statsArea);


    var heroInfo =  document.createElement('textarea');
    heroInfo.setAttribute('class', 'heroInfo');
    heroInfo.style.height = '160px';
    heroInfo.style.width = '200px';
    heroInfo.style.marginTop = '10px';
    heroInfo.style.backgroundColor = 'white';
    heroInfo.style.overflowY = 'hidden';
    heroInfo.style.resize = 'none';
    heroInfo.readOnly = 'true';
    heroInfo.disabled = 'true';
    heroInfo.style.cursor = "default";
    document.body.appendChild(heroInfo);

    var monsterInfo =  document.createElement('textarea');
    monsterInfo.setAttribute('class', 'monsterInfo');
    monsterInfo.style.height = '160px';
    monsterInfo.style.width = '200px';
    monsterInfo.style.backgroundColor = 'white';
    monsterInfo.style.overflowY = 'hidden';
    monsterInfo.style.resize = 'none';
    monsterInfo.readOnly = 'true';
    monsterInfo.disabled = 'true';
    monsterInfo.style.cursor = "default";
    document.body.appendChild(monsterInfo);

    statsArea.appendChild(monsterInfo);
    statsArea.appendChild(heroInfo);




    /*CONSTRUCTOR FUNCTIONS*/

    function character (name, hitpoints, armorclass, attackbonus, weapondamage) {
    this.name = name;
    this.hitPoints = hitpoints;
    this.armorClass = armorclass;
    this.attackBonus = attackbonus;
    this.weaponDamage = weapondamage;
    this.stats = function(){
    return  this.name + "\n" +
    "Hit Points: " + this.hitPoints + "\n" +
            "Armor Class: " + this.armorClass + "\n" +
            "Attack Bonus: " + this.attackBonus + "\n" +
            "Weapon Damage: " + this.weaponDamage;
    };
    this.alive = true;
    this.reset = function (){
        this.hitPoints = hitpoints;
        this.armorClass = armorclass;
        this.attackBonus = attackbonus;
        this.weaponDamage = weapondamage;
      };
}

var Arquer = new character("Arquer", 15, 6, 5, 8);  

function selectMonster () {
    var werewolf = new character("Werewolf", 15, 4, 4, 3);
    var goblin = new character("Goblin", 15, 4, 4, 3);
    switch(Math.floor(Math.random()*2)+1){
      case 1: return werewolf;
      case 2: return goblin;
    }
 }


var buttonAttack= document.createElement('input');
buttonAttack.setAttribute('type','button');
buttonAttack.setAttribute('value','Attack');
document.body.appendChild(buttonAttack);

var current_monster = selectMonster();

heroInfo.value = Arquer.stats() + "\n" + "Alive: " + Arquer.alive;
monsterInfo.value = current_monster.stats() + "\n" + "Alive: " + current_monster.alive; 

buttonAttack.onclick = function(){
if (current_monster.hitPoints <= 0){current_monster = selectMonster();monsterInfo.value = current_monster.stats() + "\n" + "Alive: " + current_monster.alive;}  
else{battle(Arquer, current_monster);}
};


function battle (hero, monster){

  if(hero.alive===true && monster.alive===true){
    var heroIniciative = Math.floor(Math.random()*20)+1;
    var monsterIniciative = Math.floor(Math.random()*20)+1;
    var attacker;
    var defender;
    var attackerInfo;
    var defenderInfo;
    /*INICIATIVE ROLL*/
    if (heroIniciative >= monsterIniciative){
        attacker = hero;
        defender = monster;
        attackerInfo = heroInfo;
        defenderInfo = monsterInfo;
        textInfo.value += attacker.name + " attacks first!: " + heroIniciative + " vs " + monsterIniciative + "\n";
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    else {
    attacker = monster;
    defender = hero;
    attackerInfo = monsterInfo;
    defenderInfo = heroInfo;
        textInfo.value += attacker.name + " attacks first!: " + monsterIniciative + " vs " + heroIniciative + "\n",
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    check_defeat(attacker, defender, attackerInfo, defenderInfo);
  }
  else {reset (hero, monster);
}
}


function attack (attacker, defender, attackerInfo, defenderInfo){
    var d20 = Math.floor(Math.random()*20)+1;
    var d_wp = Math.floor(Math.random()*attacker.weaponDamage)+1;
    /*ROUND ONE*/
    if (d20+attacker.attackBonus>defender.armorClass){
        textInfo.value += attacker.name +" d20+" + attacker.attackBonus+": " + (d20+attacker.attackBonus)+ " vs AC " + defender.armorClass + "\n" + attacker.name +" hits! d" + attacker.weaponDamage + ": " + d_wp  + "\n";
        textInfo.scrollTop = textInfo.scrollHeight;
        defender.hitPoints = defender.hitPoints - d_wp;
        defenderInfo.value = defender.stats();
        defenderInfo.append("\n" + "Alive: " + defender.alive);
        }
    else {
        textInfo.value += attacker.name + " misses! d20+" + attacker.attackBonus+": " + (d20+attacker.attackBonus)+ " vs AC " + defender.armorClass;
        textInfo.scrollTop = textInfo.scrollHeight;
        defenderInfo.value = defender.stats();
        defenderInfo.append("\n" + "Alive: " + defender.alive);
   }}


function check_defeat (attacker, defender, attackerInfo, defenderInfo) {
    if (attacker.hitPoints <= 0){
        attacker.hitPoints = 0;
        attacker.alive = false,
        attackerInfo.value = attacker.stats();
        attackerInfo.append("\n" + "Alive: " + attacker.alive);
        textInfo.value += "\n" +defender.name + " killed " + attacker.name + "!";
        textInfo.scrollTop = textInfo.scrollHeight;
     } 
    if (defender.hitPoints <= 0){
        defender.hitPoints = 0;
        defender.alive = false,
        defenderInfo.value = defender.stats();
        defenderInfo.append("\n" + "Alive: " + defender.alive);  
        textInfo.value += "\n" + attacker.name + " killed " + defender.name + "!";
     }
}

function reset (hero, monster) {
    if (hero.alive===false){
        hero.reset();
        hero.alive = true;
        heroInfo.value = hero.stats();
        heroInfo.append("\n" + "Alive: " + hero.alive);  
    }
    if (monster.alive===false){
        monster.reset();
        monster.alive = true;
        monsterInfo.value = monster.stats(); 
        monsterInfo.append("\n" + "Alive: " + monster.alive);  
    }
}  
}

(For some reason it doesn't work in jsfiddle). The problem is in the function battle.

function battle (hero, monster){

  if(hero.alive===true && monster.alive===true){
    var heroIniciative = Math.floor(Math.random()*20)+1;
    var monsterIniciative = Math.floor(Math.random()*20)+1;
    var attacker;
    var defender;
    var attackerInfo;
    var defenderInfo;
    /*INICIATIVE ROLL*/
    if (heroIniciative >= monsterIniciative){
        attacker = hero;
        defender = monster;
        attackerInfo = heroInfo;
        defenderInfo = monsterInfo;
        textInfo.value += attacker.name + " attacks first!: " + heroIniciative + " vs " + monsterIniciative + "\n";
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    else {
    attacker = monster;
    defender = hero;
    attackerInfo = monsterInfo;
    defenderInfo = heroInfo;
        textInfo.value += attacker.name + " attacks first!: " + monsterIniciative + " vs " + heroIniciative + "\n",
        textInfo.scrollTop = textInfo.scrollHeight;
        attack(attacker, defender, attackerInfo, defenderInfo);
        attack(defender, attacker, defenderInfo, attackerInfo);
    }
    check_defeat(attacker, defender, attackerInfo, defenderInfo);
  }
  else {reset (hero, monster);
}
}

When I call it, it just execute the first function attack, but not the second one.

buttonAttack.onclick = function(){
if (current_monster.hitPoints <= 0){current_monster = selectMonster();monsterInfo.value = current_monster.stats() + "\n" + "Alive: " + current_monster.alive;}  
else{battle(Arquer, current_monster);}
};
Share Improve this question edited May 1, 2015 at 15:03 Paul asked May 1, 2015 at 14:03 PaulPaul 852 gold badges2 silver badges11 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 4

When executing a function you don't have to write function up front.

try

function one(){
     two(a,b);
     two(c,d);  
}

It should work -

function two(a,b){
  console.log(a+b);
}

function one(){
 two(1,2);
 two(3,4);  
}

one();

output:

2

7

the first function two is executed

This is highly unlikely; why do you believe it was executed? Before you call anything, or anything is executed, there is a syntax error, probably something like "Unexpected token ;", because the syntax

function two(a, b);

is invalid; function definitions must have a body in curly braces. You would see the error if you looked at the console; did you? It must be:

function two(a, b) { }

But apparently you just want to call the function, in which case you should use the basic function call syntax of two(a, b). function is for defining functions, not calling them.

Best and easiest solution is to use Promist.all() with nodejs

Example

let _ = require('underscore')

let response = ['some data','other data'] 

return Promise.all(_.map(response, function (data) { return 
functionName(data) }))
.then((response)=>{
console.log(response)
})

Result

['response1','response2']

you can simply call a function twice or any time you want just remember its arguments and its data types since you are not forced to put data type on function call but if there is wrong data type it can create serious mess.

//currently in a mess

本文标签: javascriptCalling the same function twice with different parametersStack Overflow