vendredi 29 juillet 2016

Creating random numbers with time out javascript

Have a function thats emulated server request

function request(val, callback) {
  setTimeout(function() {
  callback(val * val);}, Math.randInt(1000));}

val – integer value; callback – function that will be executed after some random delay. It has one integer parameter.

I need folowing: generate number and emulate server timeout by this function

try to this

    <script>

       var a = function randomInteger(max) {
        var rand = 0 + Math.random() * (max + 1 - 0 -1);
        rand = Math.floor(rand);
        return rand;
        };

        var q = a(50);
        var g = request(q);
        function request(val, callback) {
      setTimeout(function() {
      callback(val * val);}, 3000);}
console.log(g);
        </script>

but have answer: Uncaught TypeError: callback is not a function - after 3 seconds. Need to generate number and after some timeout show it, using function at the top

and I dont understand what means Math**.randInt**(1000));




Aucun commentaire:

Enregistrer un commentaire