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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:09:09+00:00 2026-06-03T11:09:09+00:00

Take a look at this jsFiddle Code also listed below: window.MyView = Backbone.View.extend({ ticks:

  • 0

Take a look at this jsFiddle

Code also listed below:

window.MyView = Backbone.View.extend({

ticks: 0,
initialize: function() {
    //window.setInterval(this.onTimerTick, 1000); // arghhh.. can't understand the 'this' scoping       
    window.setInterval(this.onTimerTick2, 1000);  // Works great with globals       
},

render: function() {
    this.$el.text(this.ticks);            
},

onTimerTick: function() {  // Trouble with this
    this.ticks++;
    this.render();
},

onTimerTick2: function() {  // Using globals
    window.ticks2++;
    $('#count').text(window.ticks2);
}
});

window.My = new MyView({ el: $("#count") });
window.ticks2 = 0;

Looking at the code, you see I would like to use the onTimerTick function, but becouse I can’t figure out how to get from the window-this to the My-this, I must use the approach seen in onTimerTick2. (usually I get around with a that=this, but in this case it is not enough)

Thanks for any attemt to make me understand this(!)

Thanks

Larsi

  • 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-03T11:09:12+00:00Added an answer on June 3, 2026 at 11:09 am

    When passing this.onTimerTick2 to setTimeout, the function will be called with this being bound to the global object, not your object.

    If underscore.js is available (according to @ori it is), you can use _.bind() to lock this to the correct object when called:

    window.setInterval(_.bind(this.onTimerTick, this), 1000);
    

    Here are some solutions that do not depend on a library:

    // Works in all browsers
    var self = this;
    window.setInterval(function() {
        self.onTimerTick();
    }, 1000);
    

    With a modern JS engine you can also use Function.bind() to keep the correct this:

    // Requires a modern JS engine
    window.setInterval(this.onTimerTick.bind(this), 1000);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at this simple code: http://jsfiddle.net/kerp3/ The box has an inner
Take a look at this code: public class Test { public static void main(String...
take a look at this code: $(document).ready(function() { document.getElementById(sliderId).onmousedown = sliderMouseDown; }); function sliderMouseDown()
Take a look at this jsfiddle . I've used the jQuery dialogs before and
Well Im stucked at this point.please take a look at this http://jsfiddle.net/karthik64/5jhgF/3/ Okay first
Please take a look at my code: http://jsfiddle.net/XptrZ/ Why are'nt the blue divs inside
Could someone possibly take a look at this: http://jsfiddle.net/VkFRU/4/ and explain the console output
Take a look at this. http://jsfiddle.net/DTxQf/18/ When you click on the body of the
Please take a look at this fiddle: http://jsfiddle.net/KA85s/3/ JQ-UI sets first option as default.
Take a look at this; http://jsfiddle.net/k2zyz/ Why is the 100% width tab exceeding the

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.