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

  • SEARCH
  • Home
  • 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 9203671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:31:28+00:00 2026-06-17T23:31:28+00:00

I am attempting to set multiple cue points in a video. Instead of writing

  • 0

I am attempting to set multiple cue points in a video. Instead of writing each cue out, I would like to iterate over an object that has the data I need, such as the time of the cue and some info about what to do with the call back.

The problem is when I try to iterate over the object it overwrites all the cues except for the last one.

var products = myVideo.products;
var video = Popcorn('#mainVideo');

    for (product in products){
        var obj   = products[product],
            start = obj.start,
            img   = obj.image,
            $targetDiv  = $("#"+obj.targetDiv);

        video.cue( start, function(){
            $('<img>', {
            class : "isImage",
            src : 'images/' + img
            }).prependTo( $targetDiv );
        })

    }

Any help would be greatly appreciated.

  • 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-06-17T23:31:29+00:00Added an answer on June 17, 2026 at 11:31 pm

    In this code, every cue has its own callback function, but every function refers to the same variable img and the same $targetDiv. By the time they run, those variables will be set to their respective values for the last item in products.

    If you’ve ever run code through jslint and seen the warning, don’t make functions in a loop, this is why. A good way to do what you’re trying to do is to put those variables inside of a function that gets called immediately and returns another function, which is your callback. Like this:

    function makeCallback(obj) {
        return function() {
            $('<img>', {
            class : "isImage",
            src : 'images/' + obj.img
            }).prependTo( $("#"+obj.targetDiv) );
        };
    }
    
    for (var product in products) {
        var obj = products[product];
        video.cue( obj.start, makeCallback( obj ) );
    }
    

    Alternatively, you can use forEach, which does the same thing under the hood. (Popcorn provides its own version, which handles both arrays and objects.)

    Popcorn.forEach(products, function(obj) {
        video.cue( start, function(){
            $('<img>', {
            class : "isImage",
            src : 'images/' + obj.img
            }).prependTo( $("#"+obj.targetDiv) );
        });
    });
    

    I should note that you have another problem in this code, which is that you have Popcorn creating a new image every time you pass through the cue point. So, if the user ever skips back to replay some of the video, the images will start to pile up. Also, the images don’t start loading until right when they become visible, so if there’s a slow-ish network connection, the images may not show up until it’s too late.

    The best way to handle these is usually to create your images ahead of time but make them invisible with CSS, and then have the Popcorn events make them visible at the right time. You might consider using the image plugin, which will do most of your heavy lifting for you.

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

Sidebar

Related Questions

I'm fairly new to WCF's and would like a bit of clarification. I'm attempting
I'm attempting to set multiple ACLs in one batch request from appengine (JAVA). I
I am attempting to get multiple String variables set with one println. Here is
I am attempting to split out the Hibernate DAO and Model Object layer from
Possible Duplicate: Creating multiple numbers with certain number of bits set I'm attempting to
I am attempting to create multiple, serial writing sessions with AVAssetWriter. Once I've completed
I am attempting to set up multiple datasources in my standalone.xml file. I have
I'm attempting to set an editor delegate on my QTableView specific to each row
Attempting to set up an automated texting service for customers, where people can text
I'm attempting to set up Pinned Site features for my project. The trouble is

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.