Giter Site home page Giter Site logo

Comments (12)

oucil avatar oucil commented on July 19, 2024

I've got a temporary fix for the above problem... adding idle_timer.children().remove(); right after var idle_timer.... ensures that none of the TC instance elements are in place, when the new one is instantiated.

With regard to the problem itself, I believe that the scope of the script that removes any previous elements is local, where in this specific case, the scope of the actual timer is in window.parent.document and so it can't actually find the timer when it's destroying the old ones.

from timecircles.

wimbarelds avatar wimbarelds commented on July 19, 2024

The problem is basically that you're loading TimeCircles.js twice. Each of them is contained within their own private scope. It's not impossible to put it be somewhere where it would be available globally, but that would mean "polluting" the global namespace.

Instead, it might be possible for you to store the TimeCircles instance somewhere in .data("TC"), I'm not 100% sure that would work. I'll give it a try before I head to bed and let you know.

from timecircles.

wimbarelds avatar wimbarelds commented on July 19, 2024

It looks like the same thing holds true for jQuery. If you assign something using .data('variable', 'value') then that value only exists within that instance of jQuery. Try just using .data() that was set in the iframe, outside of the iframe. Doesn't work.

What you can do, is share the data you want to share manually. You could do (in timer.html):

window.parent.window.timer = $("div#idle_timer", window.parent.document).TimeCircles();

Then in index.html you can simply use the timer variable (which is the public class instance of TimeCircles). So you could do:

timer.addListener(function(unit, value, total) {
    // if total....
});

The problem is essentially that you're working with 2 unconnected instances of TimeCircles that don't really have any good way of communicating with one another.

from timecircles.

oucil avatar oucil commented on July 19, 2024

I was afraid of that but it doesn't sound like it's too difficult to manage, it looks like we can get around everything via a public variable that TimeCircles() is assigned to in the parent frame and access it that way, checking to see if it already exists on each page load and either loading it fresh or restarting it via the variable and TC's own functionality. Correct?

from timecircles.

wimbarelds avatar wimbarelds commented on July 19, 2024

http://git.wimbarelds.nl/TimeCircles/test.html

Is this what you're trying to do? All that "needs" to be shared is the instance list.

from timecircles.

oucil avatar oucil commented on July 19, 2024

Not exactly, that was related to the other 'issue' which I did work through, in that case I'm only showing the minute timer to start at 30 min, there's a listener so that at 10 min it changes the colour to yellow, like you've done, then at 5 min it changes to red, then at 1 min it changes to bright red, and switches the circle timer to seconds only. It's a series of 4 nested changes.

In this case, it's the scope of the instance list that's the issue, because I was attempting to change the parent instance from within the iframe, which as you noted is in a different scope, and so it was stacking them. So instead, my jerry-rig removes all child elements of the original contain then redefines it. Removing the children removes the element(s) that TC is tied to and so unloads it, then the script continues to reload it.

I'm wondering though if I can instead launch an event listener on the parent iframe to track changes to the 'src' attribute and reload it that way, so that the children wouldn't be responsible for reloading the TC instance.... hmmm brain smoking..... That would avoid ALL of the scope issues that this causes and it would probably greatly simplify things....

from timecircles.

wimbarelds avatar wimbarelds commented on July 19, 2024

In the example I linked the scope issue is resolved. The time circles are created in the IFrame's javascript and the event listener is then added in the top frame.

from timecircles.

oucil avatar oucil commented on July 19, 2024

I'm not sure it is, when I right clicked in the iframe and refreshed only it, the timer stopped counting all together, rather than restarting from 15 seconds. Our scenario is the parent remains essentially static other than some ajax back and forth, and the iframe is where all the business logic is done. Loading a page in the iframe extends the session naturally, but the timer is in the parent and thus needed to be updated for every time a new page is loaded in the frame to reflect that new expiry.

I haven't had a chance to test it, but I think that an event listener on the iframe itself would be ideal, no more collisions, no more scope issues, just need to be sure that it sees all changes. I'll be sure and post an updated example back up here when I'm done for reference though, might help someone else trying to do the same.

from timecircles.

wimbarelds avatar wimbarelds commented on July 19, 2024

The reason that happened was because TimeCircles makes use of window.setInterval. When you refresh a frame, the old "window" no longer exists, neither will the setInterval function. Replacing setInterval and clearInterval with window.top.setInterval and window.top.clearInterval (and the same with setTimeout) solved that issue.

Edit: Mind you. Just setting .data('timer', 20) and doing another .TimeCircles() will not reinitialize the TimeCircles. If you also want that, adding a .start() should do the trick.
Edit 2: Granted, that will only set the timer back to it's original value. The colors will still be whatever they were last set to. If you really want to reset the TimeCircles, you should destroy them before you create them again.

from timecircles.

oucil avatar oucil commented on July 19, 2024

Good catch re: the setInterval! I should have a chance a little later tonight to test out the parent window only solution and will get back. And yet again, thanks for your input and attention :)

from timecircles.

oucil avatar oucil commented on July 19, 2024

Ok here's a working script that is bound to the parent iframe only, will reload every time the iframe loads a new page, runs for 30 min starting at blue, changes to orange at 10 min and red at 5 min, and switches from Minutes only to Seconds only at 1 min remaining...

$('#some_iframe').load(function(){
    var idle_timer = $('div#idle_timer'); 
    if (idle_timer.TimeCircles != null) { idle_timer.TimeCircles().destroy(); } 
    idle_timer.data('timer',1800); 
    idle_timer.TimeCircles({ time: { Days: { show:false }, Hours: { show:false }, Minutes: { color: '#FFF' }, Seconds: { show:false } }, circle_bg_color: '#4D8DC1' }).addListener( 
        function(unit,value,total) { 
            if (total <= 600) { 
                idle_timer.data('timer',600); 
                idle_timer.TimeCircles({ time: { Days: { show:false }, Hours: { show:false }, Minutes: { color: '#FFF' }, Seconds: { show:false } }, circle_bg_color: '#F90' }).addListener( 
                    function(unit,value,total) { 
                        if (total <= 300) { 
                            idle_timer.data('timer',300); 
                            idle_timer.TimeCircles({ time: { Days: { show:false }, Hours: { show:false }, Minutes: { color: '#FFF' }, Seconds: { show:false } }, circle_bg_color: '#D00' }).addListener( 
                                function(unit,value,total) { 
                                    if (total <= 60) { 
                                        idle_timer.data('timer',60); 
                                        idle_timer.TimeCircles({ time: { Days: { show:false }, Hours: { show:false }, Minutes: { show:false }, Seconds: { color: '#FFF' } }, circle_bg_color: '#D00' }).addListener( 
                                            function(unit,value,total) { 
                                                alert('Times up!');
                                            }
                                        ); 
                                    }
                                }
                            );
                        }
                    }
                );
            }
        }
    );
});

P.S. I inverted the colours a bit to be more visible on a dark background.
P.P.S. This should obviously be loaded in a .ready() block

from timecircles.

wimbarelds avatar wimbarelds commented on July 19, 2024

I assume this marks this issue resolved.

from timecircles.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.