Draaken
Draaken

Reputation: 63

Need to pass a parameter to class method being used by setInterval

I am having trouble with the setInterval method in the sense that I need to pass its first parameter (the function being set to an interval) a parameter of its own.

I've included the class in which I am trying to achieve this.

Granted the function itself works and I am getting a response from ajax, but it doesn't know where to put it. Anyone know how to achieve this?

function message_console(log_id, person_2_msg_id) {
    this.user_id = log_id;
    this.person_messaged_id = person_2_msg_id;
    this.input_id = "send_to_" + person_2_msg_id;
    this.messages_id = "chat_with_" + person_2_msg_id;

    this.get_messages = function get_messages(id) { // remember to add the id as parameter
                                                    // to this function that will receive
                                                    // this responseText
        //ajax here - get messages
        var x = new XMLHttpRequest();
        x.onreadystatechange = function() {
            if (x.readyState == 4 && x.status == 200) {
                _(id).innerHTML = x.responseText;
            }
        }
        x.open("POST", "parse/private_msg.php");
        x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        x.send("get_messages=true&to=" + person_2_msg_id);
    }

    setInterval(this.get_messages, 3000);   // need to figure out how to pass para here - with
                                            // the function as a variable and in oop - not sure
                                            // how - ask adam asap
    setInterval(this.get_messages(parameter), 3000);    //this does not work

    this.container += '<div class="msg_module">';
    this.container += '<div class="message" id="' + this.messages_id + '">' +
        this.get_messages(this.messages_id) + '</div>';
    this.container += '<input type="text" onkeypress="submit_private_msg(event,\'' +
        this.user_id + '\',\'' + this.person_messaged_id + '\',\'' + this.messages_id +
        '\',\'' + this.input_id + '\')" class="msg_head" id="' + this.input_id + '" >';
    this.container += '</div>';
}

Upvotes: 1

Views: 86

Answers (3)

James Yang
James Yang

Reputation: 1416

see MDN document here, the syntax below:

var intervalID = window.setInterval(func, delay[, param1, param2, ...]);

var intervalID = window.setInterval(code, delay);

option 1:

setInterval( this.get_messages,3000, parameter);

options 2:

setInterval( function(){ this.get_messages.call(this, parameter) } ,3000 );

Function call method see here

Upvotes: 1

leo.fcx
leo.fcx

Reputation: 6467

You can wrap your function call in another anonymous function as follows:

var me = this;
setInterval(function(){
    me.get_messages(parameter);
}, 3000);

Note that we are using a me variable which makes reference to the this of the first function.

Upvotes: 1

Buzinas
Buzinas

Reputation: 11725

You can do:

setInterval(function() { this.get_messages(parameter); }.bind(this), 3000);

Upvotes: 1

Related Questions