Automatické odeslání AJAX signálu po určitém časovém intevalu + zvukový signál

jAkErCZ
Člen | 321
+
0
-

Ahoj,
dle starého TÁMATU jsem chtěl využít script pro určitém časovém intervalu reload snippetu + přidat zvukový signál ale nějak mi to hapruje.

<script>
    (function($, undefined){
        $.nette.ext('autorefresh',{
            load:   function(rh){
                $(this.autorefreshSelector).each(function(){
                    var url = $(this).attr("data-refresh-url");
                    var refreshTime = $(this).attr("data-refresh-time");
                    this.reloadData(url,refreshTime);
                });
            },
            success: function (payload){
                console.log('jede');
                if (payload.snippets) {
                    this.updateSnippets(payload.snippets);
                }
            }
        },{
            reloadData: function(url,refreshTime){
                $.nette.ajax({
                    type: 'GET',
                    url: url,
                    success:    function(payload){
                        this.updateSnippets(payload.snippets);
                        setInterval(function(){
                            this.reloadData(url,refreshTime);
                        },refreshTime * 1000);
                        playSound();
                    }
                });
                return;
            },
            autorefreshSelector:    ".autorefresh",
        });
    })(jQuery);

    function playSound()
    {
        var audio = new Audio('http://www.rangde.org/static/bell-ring-01.mp3');
        audio.play();
    }
</script>

Chyba console

?_fid=d3eb:548 Uncaught TypeError: this.reloadData is not a function
    at HTMLElement.<anonymous> (?_fid=d3eb:548)
    at Function.each (jquery.min.js:14)
    at jQuery.fn.init.each (jquery.min.js:14)
    at Object.load (?_fid=d3eb:545)
    at Function.<anonymous> (nette.ajax.js:42)
    at Function.each (jquery.min.js:14)
    at Object.fire (nette.ajax.js:40)
    at nette.load (nette.ajax.js:144)
    at nette.init (nette.ajax.js:134)
    at HTMLDocument.<anonymous> (?_fid=d3eb:487)
(anonymous) @ ?_fid=d3eb:548
each @ jquery.min.js:14
each @ jquery.min.js:14
load @ ?_fid=d3eb:545
(anonymous) @ nette.ajax.js:42
each @ jquery.min.js:14
fire @ nette.ajax.js:40
load @ nette.ajax.js:144
init @ nette.ajax.js:134
(anonymous) @ ?_fid=d3eb:487
fire @ jquery.min.js:25
fireWith @ jquery.min.js:25
ready @ jquery.min.js:25
completed @ jquery.min.js:25

Proč se to děje i když tam funkce reloadData je.

Díky

David Matějka
Moderator | 6445
+
0
-

Uvnitř jquery.each odkazuje this na ten domelement (a v té funkci se s tím taky tak pracuje). Takže si this z té nadřazené scope (ve funkci load) ulož do proměnné a volej tu.

jAkErCZ
Člen | 321
+
0
-

David Matějka napsal(a):

Uvnitř jquery.each odkazuje this na ten domelement (a v té funkci se s tím taky tak pracuje). Takže si this z té nadřazené scope (ve funkci load) ulož do proměnné a volej tu.

Mám dotaz mám tedy

<script>
    (function($, undefined){
        $.nette.ext('autorefresh',{
            load:   function(rh){
                $(this.autorefreshSelector).each(function(){
                    var url = $(this).attr("data-refresh-url");
                    var refreshTime = $(this).attr("data-refresh-time");
                    reloadData(url,refreshTime);
                });
            },
            success: function (payload){
                console.log('jede');
            }
        },{
            autorefreshSelector:    ".autorefresh",
        });
    })(jQuery);

    function reloadData(url,refreshTime) {
        $.nette.ajax({
            type: 'GET',
            url: url,
            success:    function(payload){
                setInterval(function(){
                    this.reloadData(url,refreshTime);
                },refreshTime * 1000);
                playSound();
            }
        });
    }

    function playSound()
    {
        var audio = new Audio('http://www.beepzoid.com/ringtones/Short-single-ring.mp3');
        audio.play();
    }
</script>

Už to funguje jak má ale neustále to načítá dokola skoro po 1 vteřině ale přitom mám definované refreshTime

n:snippet="notifi" data-refresh-url="{plink updateNotification!, $user->identity->id}" data-refresh-time="60000"

Proč to stále načítá po vteřině?