P
P
PO6OT2015-09-22 12:34:57
JavaScript
PO6OT, 2015-09-22 12:34:57

How to update the text on the page using JS?

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>GoDaemon</title>
<link href="/style.css" rel="stylesheet" type="text/css">
</head>
<body>

<div class="block">
<div class="cell">
<div class="center">

<div class="i">Key:</div>
<script type="text/javascript"><!--
document.write('')
//--></script>

</div>
</div>
</div>

</body>
</html>

Where document.write() needs to write and update the value of the /key file every 3 seconds.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Inchin ☢, 2015-09-22
@woonem

<span class="fileData"></span>

var xhr;
setInterval(function(){ 
  xhr&&xhr.abort();
  xhr =  new XMLHttpRequest();
  //xhr.timeout = 3000; - изначально подумал я. 
  //Но ведь интервал может повториться раньше, когда браузеру в голову взбредет...
  //Так что будем юзать abort
  xhr.onreadystatechange = function(){
     if(xhr.readyState == 4){
        document.querySelector(".fileData").textContent = xhr.responseText;
     }
  }
  xhr.open('GET', '/key', true);
  xhr.send();

}, 3000);

D
Dmitry Pyrkin, 2015-09-22
@ps1panda

<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>GoDaemon</title>
<link href="/style.css" rel="stylesheet" type="text/css">
</head>
<body>

<div class="block">
<div class="cell">
<div class="center">

<div class="i">Key:</div>
<script type="text/javascript">

function timeout() {
 document.write('text')
}
setTimeout(timeout, 2000)
</script>

</div>
</div>
</div>

</body>
</html>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question