Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何计算函数被唯一调用的次数_Javascript - Fatal编程技术网

Javascript 如何计算函数被唯一调用的次数

Javascript 如何计算函数被唯一调用的次数,javascript,Javascript,我不熟悉javascript,并试图从头开始学习。 我有一个脚本,我可以用单选按钮做两种不同的计算,面积和周长。 我想显示人们计算不同计算的次数。 若我用鼠标点击按钮,那个么它会把所有的东西都算在一起,但我怎样才能把它们分开呢 document.getElementById("calculate").addEventListener("click", function() { var button = document.getElementById("calculate"); var x

我不熟悉javascript,并试图从头开始学习。 我有一个脚本,我可以用单选按钮做两种不同的计算,面积和周长。 我想显示人们计算不同计算的次数。 若我用鼠标点击按钮,那个么它会把所有的东西都算在一起,但我怎样才能把它们分开呢

document.getElementById("calculate").addEventListener("click", function() {
  var button = document.getElementById("calculate");
  var x, text;
  x = document.getElementById("radius").value;
  if (document.getElementById("area").checked) {
    if (isNaN(x) || x < 1 || x > 100) {
      text = "Fel radie";
      document.getElementById('radius').value = '';
    } else {
      let radius = document.getElementById("radius").value;
      area = Math.PI * radius * radius;
    }
    if (document.getElementById("areaoutput").innerHTML = text);
    else(document.getElementById("areaoutput").innerHTML = "Area = " + area);
  }


  if (document.getElementById("circumference").checked) {
    let radius = document.getElementById("radius").value;
    if (isNaN(x) || x < 1 || x > 100) {
      text = "Fel radie";
      document.getElementById('radius').value = '';
    } else {
      let radius = document.getElementById("radius").value;
      circumference = Math.PI * 2 * radius;
    }
    if (document.getElementById("circoutput").innerHTML = text);
    else(document.getElementById("circoutput").innerHTML = "Circumference = " + circumference);
  }
  if (document.getElementById("area").checked) {
    var count = localStorage.on_load_counter || 0;
    var button = document.getElementById("calculate");
    var display = document.getElementById("areacount");

    button.onclick = function() {
      localStorage.on_load_counter = display.innerHTML = ++count;
    }
  }
  if (document.getElementById("circumference").checked) {
    var count = localStorage.on_load_counter || 0;
    var button = document.getElementById("calculate");
    var display = document.getElementById("circcount");
  }
  button.onclick = function() {
    localStorage.on_load_counter = display.innerHTML = ++count;
  }

}, false);
document.getElementById(“计算”).addEventListener(“单击”,函数)(){
var按钮=document.getElementById(“计算”);
变量x,文本;
x=document.getElementById(“半径”).value;
if(document.getElementById(“area”).checked){
如果(isNaN(x)| | x<1 | | x>100){
text=“自由半径”;
document.getElementById('radius')。值='';
}否则{
让radius=document.getElementById(“radius”).value;
面积=Math.PI*半径*半径;
}
if(document.getElementById(“areaoutput”).innerHTML=text);
else(document.getElementById(“areaoutput”).innerHTML=“Area=“+Area”);
}
if(选中document.getElementById(“圆周”)){
让radius=document.getElementById(“radius”).value;
如果(isNaN(x)| | x<1 | | x>100){
text=“自由半径”;
document.getElementById('radius')。值='';
}否则{
让radius=document.getElementById(“radius”).value;
周长=数学PI*2*半径;
}
if(document.getElementById(“circoutput”).innerHTML=text);
else(document.getElementById(“circoutput”).innerHTML=“percentral=”+percentral);
}
if(document.getElementById(“area”).checked){
var count=localStorage.on|u load|u计数器| 0;
var按钮=document.getElementById(“计算”);
var display=document.getElementById(“areacount”);
button.onclick=函数(){
localStorage.on_load_counter=display.innerHTML=++count;
}
}
if(选中document.getElementById(“圆周”)){
var count=localStorage.on|u load|u计数器| 0;
var按钮=document.getElementById(“计算”);
var display=document.getElementById(“circcount”);
}
button.onclick=函数(){
localStorage.on_load_counter=display.innerHTML=++count;
}
},假);

试试这样做

var组合sdone=[]

然后,每次用户进行计算时,您都会将一个对象添加到
CombinationsOne

例如,如果它们的半径为5,但未检查周长,则应该这样做

var combination = {
    radius: 5,
    circumference: false
}

if(!combinationsDone.includes(combination)){
    combinationsDone.push(combination);
}

这样做的目的是确保在添加组合之前没有记录组合。然后,当您想要获得他们所做的不同组合的数量时,只需用户
combinationsDone.length

尝试这样做

var组合sdone=[]

然后,每次用户进行计算时,您都会将一个对象添加到
CombinationsOne

例如,如果它们的半径为5,但未检查周长,则应该这样做

var combination = {
    radius: 5,
    circumference: false
}

if(!combinationsDone.includes(combination)){
    combinationsDone.push(combination);
}

这样做的目的是确保在添加组合之前没有记录组合。然后,当您想要获得他们所做的不同组合的数量时,只需用户
combinationsDone.length
就可以使用全局变量。每次调用函数时都会增加

var myFuncCalls = 0;

function myFunction()
{
    myFuncCalls++;
    alert( "I have been called " + myFuncCalls + " times" );
}

可以使用全局变量。每次调用函数时都会增加

var myFuncCalls = 0;

function myFunction()
{
    myFuncCalls++;
    alert( "I have been called " + myFuncCalls + " times" );
}

请修正缩进,以便我们更容易理解您的代码在做什么。
if(a=b);else(a=c)似乎无法达到您的预期效果。请修复缩进,以便我们更容易理解您的代码正在执行的操作。
如果(a=b);else(a=c)
似乎不能满足您的期望。