1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Having trouble with XF AutoValidator and SetInterval...

Discussion in 'XenForo Development Discussions' started by Jaxel, Jan 12, 2014.

  1. Jaxel

    Jaxel Well-Known Member

    So I have a form:
    Code:
    <form action="{xen:link 'streams/streams'}" method="post" class="channelSide AutoValidator StreamsUpdater">
        <xen:include template="EWRrio_Sidebar_Streams" />
       
        <xen:if is="{$visitor.user_id}==1">
            <input type="submit" value="{xen:phrase submit}" name="submit" accesskey="s" class="button primary" />
        </xen:if>
    </form>
    As you can see, its a simple form. It includes the template "EWRrio_Sidebar_Streams". In addition, there is a button that only shows when I am viewing the form. Otherwise, no one else sees the submit button. When submit is clicked, it runs the following action:

    Code:
    public function actionStreams()
    {
        $viewParams = array(
            'streams' => $this->getModelFromCache('EWRrio_Model_Streams')->getStreams(1, 5),
        );
    
        return $this->responseView('EWRrio_ViewPublic_Streams', 'EWRrio_Sidebar_Streams', $viewParams);
    }
    However, you will notice that on the form, I have added a jQuery register for "StreamsUpdater":
    Code:
    XenForo.StreamsUpdater = function($form)
    {
        $form.bind('AutoValidationComplete', function(e)
        {
            if (e.ajaxData.templateHtml)
            {
                $(e.ajaxData.templateHtml).xfInsert('replaceAll', '.streamsSide');
            }
        });
    }
    Basically it just takes the templateHtml generated by the form post, and replaces the contents of "EWRrio_Sidebar_Streams" with the new contents.

    When I click the submit, it works perfectly fine. Its just an updater that updates the contents of the specific sidebar element.

    However, what I would like to do is have this run automatically, WITHOUT having to click a submit button, once every 10 minutes or so. I had it working in the past, but now I am getting an error. In the past, I was able to accomplish this with a slight change to my jQuery register:
    Code:
    XenForo.StreamsUpdater = function($form)
    {
        setInterval(function() { $form.submit(); }, 600000);
    
        $form.bind('AutoValidationComplete', function(e)
        {
            if (e.ajaxData.templateHtml)
            {
                $(e.ajaxData.templateHtml).xfInsert('replaceAll', '.streamsSide');
            }
        });
    }
    You can see a new line of code in this script:
    Code:
    setInterval(function() { $form.submit(); }, 600000);
    Seems like it should work right? Set the form to automatically submit every 10 minutes. However, for some reason, this doesnt work. When the setInterval gets fired, instead of it replacing the templateHtml like it would do when I click the submit button, XF instead pops up a javascript error (says error is located in the console).

    So I go to the console... and I see a printout of the templateHtml. Why is it doing this? How is this setInterval any different from actually clicking the submit button?
     
  2. Jaxel

    Jaxel Well-Known Member

    And now it is randomly working again... I am so confused.
     

Share This Page