-1

Rather than waiting 5 seconds my use of setTimeout seems to be executing straight away:

window.setTimout(getNegativeErrs(errors), 5000);

  function getNegativeErrs(ers) {
    for( i in errors ) {
      var errobj = getByValue(dataLayer, errors[i]);
      if(!errobj) {
        dataLayer.push({
          'event': 'negative_errors',
          'unseen': errors[i],
          'module': 'registration'
        })
      }
    }
  }

Must the function being passed be of a specific format? I really don't know what to edit here or where to start looking?

Doug Fir
  • 19,971
  • 47
  • 169
  • 299
  • 2
    You are doing it wrong. Write a function expression as first argument inside of which you call your target function (with its arguments). `setTimeout(function(){foo(bar)}, 5000);` – deostroll Aug 08 '16 at 02:27

1 Answers1

2

Your function getNegativeErrs() is executing right away and is using its return value as the callback.

The right way:

window.setTimeout(function() {
    getNegativeErrs(errors)
    }, 5000);

  function getNegativeErrs(ers) {
    for( i in errors ) {
      var errobj = getByValue(dataLayer, errors[i]);
      if(!errobj) {
        dataLayer.push({
          'event': 'negative_errors',
          'unseen': errors[i],
          'module': 'registration'
        })
      }
    }
  }
Doug Fir
  • 19,971
  • 47
  • 169
  • 299
Robert
  • 10,126
  • 19
  • 78
  • 130