0

I try to request an URL every 5 seconds. The following code is returning ReferenceError: Can't find variable: validateUserTime

$(document).ready(function() {
  ({
    validateUserTime: function() {
      return $.get('/myurl', function(data) {});
    }
  });
  return window.setInterval((function() {
    validateUserTime();
  }), 5000);
});

I'm wondering what I'm doing wrong that is preventing a call to the method instead of doing it as a variable. Any idea?

1
  • 2
    You never define a variable validateUserTime anywhere. All you do is creating an object literal with one property but not assign it to anything. It just gets garbage-collected. Just define the variable properly: var validateUserTime = function ...;. Commented Dec 17, 2012 at 7:02

2 Answers 2

3

This simply defines an anonymous object and throws it away:

({
  validateUserTime: function() {
    return $.get('/myurl', function(data) {});
  }
});

That doesn't define a validateUserTime function or method. You want something like this:

var validateUserTime = function() {
  return $.get('/myurl', function(data) {});
};

or perhaps:

function validateUserTime() {
  return $.get('/myurl', function(data) {});
}
Sign up to request clarification or add additional context in comments.

Comments

2

In the first statement, you are using an object literal without assigning it to anything. Assign it to something to fix it.

$(document).ready(function() {
  var functions = {
    validateUserTime: function() {
      return $.get('/myurl', function(data) {});
    }
  };
  return window.setInterval((function() {
    functions.validateUserTime();
  }), 5000);
});

1 Comment

Wondering how could I perform the get request first and then wait 5000 milliseconds. This example waits for the 5000 ms first and then makes the first request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.