One Function interferes with another

Asked

Viewed 71 times

0

What I do when executing a function in javascript, it causes the previous function to start again, not accepting the changes made?

<html>
<script>
    function criartabela() {
        n = document.getElementById("ordem").value;
        a = n * n;
        for (i = 1; i <= a; i++) {
            valor = '' + i;
            conteudo = '<input type="number" id=' + valor + ' value="3"/>';
        }
        var html = "";
        html += '<table border>';
        for (var x = 1; x <= n; x++) {
            html += '<tr>';
            for (var y = 1; y <= n; y++) {
                html += '<td>' + conteudo + '</td>';
            }
            html += '</tr>';
        }
        html += '</table>';
        document.getElementById('diiiv').innerHTML = html;
    }
    function matriz() {
        criartabela();
        termo = [];
        m = '';
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= n; j++) {
                b = document.getElementById("" + valor + "").value;
                termo[i] = '\r\nA' + i + '' + j + '\r\n=' + b + '<br>';
                m = m + termo[i];
            }

        }
        document.getElementById("matriz").innerHTML = m;
    }
</script>
<body>
    <blockquote><blockquote> <!-- 2 tabs -->
            <form>
                <br><br>
                Ordem da Matriz Quadrada:   
                <input type="number" id="ordem" value=" ">
                <br><br>
            </form>
            <button onclick="criartabela()">Gerar Matriz a ser Preechida</button>
            <br><br>
            <style>ins{text-decoration:none;}</style>
            <ins id="diiiv"></ins>
            <br><br>
            <button onclick="matriz()"> Valores dos termos </button>
            <br><br>
            <style>ins{text-decoration:none;}</style>
            <ins id="matriz"></ins>
            <br><br>
        </blockquote></blockquote>
</body>
</html>
  • 7

    post your code so we can better understand what your real problem is.

  • 1

    Join the code you have or the functionality you want for the question to be complete and we can help.

  • I’m not sure I understand, but your code is full of implicit global variables, and maybe your problem is a side effect of it. Always declare the variables with var.

1 answer

0

From what I understand you want something like this:

function anterior(acao)
{

var data = 'inicio';

if (!executora(acao)) {
   for (var i=0; i<=20; i++) {
     if (i==0) {
         data='';
     }
         data += 'executou '+i+"\n";
   }
 data +='fim';
}
return data;
}

function executora(acao) 
{
 return acao;
}

var saia_anterior = anterior(true);
var saida_posterior = anterior(false);

console.log(saia_anterior+'=======>'+saida_posterior);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.