Commenting onChange Hook

Article author
Duncan from Memberstack
  • Updated

This function is a utility for handling commenting events. For example, when a new comment is made, a comment is edited, etc.

Your code goes under the // something has changed comment. There is a retry mechanism to ensure it starts listening only after Memberstack is fully loaded.

function waitForMsChannelEvents() {
    const msEvents = window.$msChannelEvents
    if (msEvents) {
        msEvents(function(data) {
            console.log(data); 
            // something has changed
        });
    } else {
        // If $msChannelEvents does not exist, wait 200ms and try again
        setTimeout(waitForMsChannelEvents, 200);
    }
}

// Start the process
waitForMsChannelEvents();

It operates as follows:

1. Initialization: The function is self-invoked, starting the process immediately upon script load.

2. Check for msChannelEvents: It checks if window.$msChannelEvents exists.

3. Event Handling: If $msChannelEvents is present, the function subscribes to it. Whenever an event occurs, the callback function logs the event data to the console. This is useful for tracking changes or actions in Memberstack.

4. Retry Mechanism: If $msChannelEvents does not exist yet (e.g., if the Memberstack script hasn't loaded), the function waits 200 milliseconds, then tries again. This loop continues until $msChannelEvents becomes available.

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.