Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7000003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:36:54+00:00 2026-05-27T20:36:54+00:00

I need help creating an efficient JS listener against an array of objects and

  • 0

I need help creating an efficient JS listener against an array of objects and a video. It works, but is really inefficient and memory intensive, especially if the array of objects is large.

I have a video that is 1 minute long and I want to display objects that are synchronized with this video on the GUI based on timecodes. For example, at 0:23, I want to display a comment.

The synchronized, timecoded objects are stored in an array like this:

comm1 = {text : "I like it", time : 23}
comm2 = {text : "I hate it", time : 48}
comm3 = {text : "I love it", time : 59}

var arr = [comm1, comm2, comm3]

I then have a Flash video player that returns the current playhead position of the video. I can call it through JavaScript like so:

var position = document.getElementById("player").currentTime();

I then created a setInterval object that loops through the array every 250 ms to display the comment like so:

function getCurrentComment(){
    for(i=0;i<arr.length;i++){
        if(position >= arr[i].time){    
            getId("comments").innerHTML = arr[i].text;              
        }
    }
}

var commListener = setInterval("getCurrentComment()", 250);

This works visually, but in reality, it continues to execute the “getId(“comments”).innerHTML = arr[i].text” line of code every 250 ms no matter what. Is there better way to do this without having to continuously loop through an array? It gets really bad when there are 100+ objects in the array. I was looking at the Observer Pattern but I don’t think it will work for this since I’m synchronizing in real time with a video.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:36:55+00:00Added an answer on May 27, 2026 at 8:36 pm

    If you can listen to the seek event, then you can do something like this:

    var arr = [
            {text : "I like it", time : 23},
            {text : "I hate it", time : 48},
            {text : "I love it", time : 59}
        ],
        ceil = arr.length - 1,
        current = 0,
        el = getId("comments"),
        commListener;
    
    function commListenerFunc()
    {
        commListener = setInterval(function ()
        {
            if (arr[current].time == position)
            {
                el.innerHTML = arr[current].text;
    
                if ( current < ceil )
                {
                    current++;
                }
                else
                {
                    clearInterval( commListener );
                }
            }
    
        }, 250);
    }
    
    // Call this function on the seek event
    function seekCallback()
    {
        clearInterval( commListener );
    
        current = 0;
    
        for ( var i = 0; i < ceil; i++)
        {
            if ( arr[i + 1].time < position )
            {
                current++;
            }
            else
            {
                break;
            }
        }
    
        commListenerFunc();
    }
    
    commListenerFunc();
    

    Please note: I did not debug this code, and it might have some issues. I’m just posting it here to give you the general idea…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need help with creating a regex for putting all values into an array!
I need help with creating Operator-Overloaded functions please. I tried 2 but I am
I need help with creating a C# method that returns the index of the
I need a little help creating a catch-all error handling page in my ICEfaces
I need help creating an object for this declare UIWebView *webView; I thought it
I need help creating a self extracting zip file that does not display anything.
I need some help creating the necessary Javascript to add values to an existing
Need some help in creating function which can create folders recursively with giving path:
I need help creating a method that allows me to create a bunch of
I need some help on creating a user profile system. I want it to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.