Michael Froter
Michael Froter

Reputation: 99

setTimeout in node.js - javascript

Here I have a code for sum 3 numbers i+j+k but I waht to know how to configure server file to work with setTimeout function... is there some restriction?

so, here is mycode:

index.jade

!!! 5
html
  head
    title Test
  body
    form(name='form1', method='post', action='')
      label(for='1')
      input#1(type='text', name='1')
      label(for='2')
      input#2(type='text', name='2')
      label(for='3')
      input#3(type='text', name='3')
      input(name='submit', type='button', value='submit') 
    span #{result}

app.js

var express = require('express');
        app = express.createServer();

app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.set("view options", { layout: false });
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});
app.use(express.bodyParser());
    app.get('/', function(req, res){
        var result;
    res.render('index', {result: ''});
});


    app.post('/', function(req, res){
      var i = req.param('1', 0);
      i = parseInt(i);
      var j = req.param('2', 0);
          j = parseInt(j);

      var k = req.param('3', 0);
         k = parseInt(k);

      var r = i+j+k;
      res.render('index', {result:r});

    });
app.listen(3010);

How etc. to block user action (click,doubleclick,rightclick) and show him a div with text "You can do any action" in first 5 seconds and after that user can do action but after 60 seconds again can't do any action... Can I do this with setTimout function or HOW???

sorry for my english

Upvotes: 0

Views: 1971

Answers (1)

chucktator
chucktator

Reputation: 1856

You cannot do this on the server side. You have to insert a client side javascript that blocks the interaction and unblocks it again after 5 sec.

The code for doing this on the client side would be something like this:

// this code should be executed when the client receives a message from the server.
var overlay = document.getElementById("your-element-id");
overlay.style.visibility = "visible";

window.setTimeout(function () {
    overlay.style.visibility = "hidden";
}, 5000);

You should take the following steps to achieve what you want:
1. The user loads the page.
2. The user receives a message from the server, stating that he is being synchronized
3. Then either after a specified time or after another message from the server you unblock the user
4. Finally you start the game

Upvotes: 1

Related Questions