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 7081037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:52:09+00:00 2026-05-28T06:52:09+00:00

I have the following code: $(document).ready(function() { $(‘.container’).ready(function() { var v = new Video($(this));

  • 0

I have the following code:

$(document).ready(function() {

    $('.container').ready(function() {

        var v = new Video($(this));
        v.load();

    });

});

How can I keep v from disposing itself? The initialized class loads 3 variables: video, source and controls. When a click or event listener calls its function and tries to access any of them it cannot, because they have since been destroyed.

How can I get around this problem?

More sample code:

$.Class('Video', {
    init: function(container) {
        this.video = container.find('.video');
        this.source = this.video[0];
        this.controls = {
            // ...
            total: container.find('.total'),
            buffered: container.find('.buffered')
        };
    },
    load: function() {
        this.source.addEventListener('progress', function() {
            var buffered = Math.floor(this.source.buffered.end(0)) / Math.floor(this.source.duration);
            this.controls.buffered.width(Math.floor(buffered * this.controls.total.width()));
        }, false);
    }
});
  • 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-28T06:52:10+00:00Added an answer on May 28, 2026 at 6:52 am

    Variables in JavaScript are function scoped, so when the callback function passed to   $(‘.container’).ready() exits, the variable you declared in that function, v, is out-of-scope and eligible for garbage collection.

    One way to avoid that is to declare a global variable and store your Video object in that. For example:

    var v;  // Global, because it is declared outside any function
    $(document).ready(function() {
        $('.container').ready(function() {
            v = new Video($(this));
            v.load();
        });
    });
    

    Of course, global variables are at least as much trouble in JavaScript as in any other language (maybe more). A best-practice is to declare exactly one global variable, an object, for your entire page/application and store any and all of your persistent data within that object. You want to give this lone global a name that is likely to be unique, so that it won’t conflict with any third-party scripts you may use. I like to use the convention used with Java namespaces of using a registered domain name associated with the work as the unique identifier. So if you or your employer owns the domain name examplename.com:

    if (! window.examplename) {
        window.examplename = {};
    }
    examplename.videos = [];
    
    ...
    
    $(document).ready(function() {
        $('.container').ready(function() {
            var v = new Video($(this));
            v.load();
            examplename.videos.push(v);
        });
    });
    

    Elsewhere in your code, access the video objects like so:

    $.each(examplename.videos, function(idx, vid) {
            vid.someMethod();
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: $(document).ready(function(){ $(.uiCloseButton).click(function(e) { e.preventDefault(); var element = $(this).parents('.uiOverlay'); alert(element);
I have the following code: $(document).ready(function() { var id = 'cbx'; var idPrefix =
I have the following code: $(document).ready(function(){ $(.yearInner).hide(); $(.year, this).hover( function () { $(.yearInner, this).slideToggle();
Under $(document).ready(function() , I have the following code: num = $(#mainContentCategoryLinksWrapper > div).size(); This
I have using the following code for developing tabs. $(document).ready(function() { $('#container-1').tabs(); } ....
I have the following code: $(document).ready(function(){ $(.datepicker).each(function() { $(this).datepicker({ beforeShowDay: function(date){ return [$(this).parent().find(span.days).html(), ]
I have the following code, see below: $(document).ready(function(){ var prefixNumber = $('.numeric').val(); if(prefixNumber.match(????){ prefixNumber.addClass('.field-error');
I have a following code: $(document).ready(function() { $('a[rel]').each(function() { $(this).qtip( { content: { text:
I have the following code: $(document).ready(function() { var $body = $(body); for (var i
I have the following code in my head jQuery(document).ready(function() { var pic = 0;

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.