Não consigo passar uma variável global pra fora da função no Javascript
Boa tarde Estou com um problema para passar valores de uma variável dentro de uma função pra fora pois preciso usar em outro arquivo.
Eu consegui passar os valores dela usando localStorage.setItem('lineCount) dentro da função e recuperando fora com getItem,mas isso não me serve pois está sendo criado cookies e quando eu retorno a fase do game para fazer novamente ele não me traz o valor novo e sim o anterior,a não ser que eu de um CTRL+R para limpar o cache.
Tentei também criar o cookie e pegar após isso excluir usando removeItem mas parece que ele está excluindo antes de pegar com isso não me exibe valor nenhum.
Eu preciso que o valor da variável seja lido de fora da função mas sempre está retornando undefined todos os valores de variáveis que estão dentro dessa função.
Já tentei retirar o var,já tentei usar o window. Mas não funciona.
BlocklyDialogs.congratulations = function() {
var content = document.getElementById('dialogDone');
// Add the user's code.
if (BlocklyGames.workspace) {
var linesText = document.getElementById('dialogLinesText');
linesText.textContent = '';
// Line produces warning when compiling Puzzle since there is no JavaScript
// generator. But this function is never called in Puzzle, so no matter.
var code = Blockly.JavaScript.workspaceToCode(BlocklyGames.workspace);
code = BlocklyInterface.stripCode(code);
var noComments = code.replace(/\/\/[^\n]*/g, ''); // Inline comments.
noComments = noComments.replace(/\/\*.*\*\//g, ''); /* Block comments. */
noComments = noComments.replace(/[ \t]+\n/g, '\n'); // Trailing spaces.
noComments = noComments.replace(/\n+/g, '\n'); // Blank lines.
noComments = noComments.trim();
var lineCount = noComments.split('\n').length;
variavel_global = noComments.split('\n').length;
var pre = document.getElementById('containerCode');
pre.textContent = code;
if (typeof prettyPrintOne == 'function') {
code = pre.innerHTML;
code = prettyPrintOne(code, 'js');
pre.innerHTML = code;
}
if (lineCount == 1) {
var text = BlocklyGames.getMsg('Games_linesOfCode1');
} else {
var text = BlocklyGames.getMsg('Games_linesOfCode2')
.replace('%1', String(lineCount));
}
linesText.appendChild(document.createTextNode(text));
}
if (BlocklyGames.LEVEL < BlocklyGames.MAX_LEVEL) {
var text = BlocklyGames.getMsg('Games_nextLevel')
.replace('%1', String(BlocklyGames.LEVEL + 1));
} else {
var text = BlocklyGames.getMsg('Games_finalLevel');
}
var cancel = document.getElementById('doneCancel');
cancel.addEventListener('click', BlocklyDialogs.hideDialog, true);
cancel.addEventListener('touchend', BlocklyDialogs.hideDialog, true);
var ok = document.getElementById('doneOk');
ok.addEventListener('click', BlocklyInterface.nextLevel, true);
ok.addEventListener('touchend', BlocklyInterface.nextLevel, true);
BlocklyDialogs.showDialog(content, null, false, true, style,
function() {
document.body.removeEventListener('keydown',
BlocklyDialogs.congratulationsKeyDown, true);
});
document.body.addEventListener('keydown',
BlocklyDialogs.congratulationsKeyDown, true);
document.getElementById('dialogDoneText').textContent = text;
};
var line = variavel_global;Discussão (4)
Carregando comentários...