+ 2

Write js var without refresh

So lets say the var x = 5, and you do document.write(x); but then later do var x = 7, how do you update the document.write to say 7?

9th Jun 2017, 7:08 PM
BGMead
BGMead - avatar
2 Respuestas
+ 12
It depends on how you change the x value, for example: --------------------------------------- // Variable var x = 5; // Value change every 200ms setInterval(function(){ document.write('Variable is: ' + x++ + '<br>' ) },200) --------------------------------------- document.write( ) must be used for testing purposes only, so... don't use it! Create a paragraph and put inside it values with the innerHTML property, is definitely easier for change a value dynamically. ;)
9th Jun 2017, 7:35 PM
Maz
Maz - avatar
+ 6
var params = { _x : 5, get x(){ return this._x; }, set x(value){ this._x = value; document.write(value); } }; params.x = 7; //writes 7 into document; params.x = 666; //writes 666 into document; console.log(params.x) //outputs 666 into console
12th Jun 2017, 4:55 PM
SANYABEAST
SANYABEAST - avatar