javascript - Change html text based on node.js server setTimeout() -


how 1 change text of div string of text after period of time using node.js , settimeout function?

the main caveat text should changed users @ same time.

let's h2 title on index page "welcome page."

5 seconds later (after server time, not after time of request), h2 title should change "let's started."

if request came in 6 seconds after server started, should see "let's started." , never see "welcome page." since h2 title should have been universally changed.

any guidance hugely appreciated.

here may try (express + socketio 1.0 example):

server.js:

var app    = require('express')(); var server = require('http').createserver(app); var io     = require('socket.io')(server);  server.listen(80);  var dir = __dirname;  app.get('/', function(req, res) {     res.sendfile(dir + '/index.html'); });  var header = 'welcome page.'; settimeout(function() {     header = 'let\'s started.';     io.emit('change_header', {         header: header     }); }, 5000);  io.on('connection', function(socket) {     socket.emit('change_header', {         header: header     }); }); 

index.html:

<!doctype html> <html> <head> <script src="/socket.io/socket.io.js"></script> <script> document.addeventlistener('domcontentloaded', function() {     var socket = io();     socket.on('change_header', function(data) {         document.getelementbyid('header').innerhtml = data.header;     }); }); </script> </head> <body>      <h2 id="header"></h2>  </body> </html> 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -