0

I have several files I'm pulling minor text from (a single word most), and then stylizing that with another script.

Currently they load and display as they should. However, the text files update at random times, so I'd like them to be reloaded, and the subsequent script applied to them again. I've tried different setTimeout as well as setInterval commands, but I think the issue is my placement or use. After several hours of research I'm certain it's just the syntax that's out of place.

This runs locally but is pulled through a program that excecutes the script as if remote. (no cross domain issues)

Here's one example segment that pulls a file and loads to the html the subsequent script reads to display:

$(function follow_pull() {
        $.ajax({
            url : "most_recent_follower.txt",
            dataType: "text",
            success : function (data) {
                $("#follow").append(data).serialize();
            },
            setTimeout(fuction(){
                follow_pull()
            }, 10000);
        });
}); 

Here's the segment that loads those files into the script to display:

$(window).ready(function ledload() {
var options = {
        pixelSize: 5, 
        stepDelay: 62, 
        horizontalPixelsCount:650,
        verticalPixelsCount:5,
        pixelRatio: 0.8,
        pathToPixelImage: 'ticker/pixel.png',
        backgroundColor: '#000',
        disabledPixelColor : '#020202',
        enabledPixelColor: '#ff522b'
    };
    $('.canvasld, .crl').leddisplay($.extend(options, {pixelSize: 3}));
        },
            setTimeout(fuction(){
                ledload()
            }, 10000););    

Any direction is appreciated. I can post the entire file if need by, but I figured someone would get what I'm doing and know how to direct me best.

For context I'm using a script that takes the text, and makes it look like an LED and scrolls as if it's a ticker. This is being used for a broadcaster on Twitch.

Snarky_one
  • 13
  • 8
  • Could you add the code where you're doing `setTimeout` / `setInterval` please? Then we can check why it's going wrong, – user184994 Jul 16 '18 at 20:39
  • I mean I can, but I don't think I'm doing any of it right. One minute. – Snarky_one Jul 16 '18 at 20:42
  • Move that setTimeout inside the `success` callback, and it should work – user184994 Jul 16 '18 at 20:45
  • It's been added, I did the Timeout Version. I figured I had to execute them separately.... if only one, then I assume on the bottom segment since it calls to the top? But How I'm not sure. – Snarky_one Jul 16 '18 at 20:45
  • What about the bottom segment? – Snarky_one Jul 16 '18 at 20:45
  • What do you mean by "the bottom segment"? – user184994 Jul 16 '18 at 20:46
  • So like this? ` $.ajax({ url : "most_recent_subscriber.txt", dataType: "text", success : function (data) { $("#sub").append(data).serialize(); setTimeout(subscriber_pull,5000); } });` – Snarky_one Jul 16 '18 at 20:46
  • There's 2 sections.... the top pulls the files, the bottom applies the code. I need to both pull the files, then apply the code to the data pulled from the files. – Snarky_one Jul 16 '18 at 20:47
  • How come in that code you just pasted it's changed to `subscriber_pull`? I thought the function was called `follow_pull`? – user184994 Jul 16 '18 at 20:48
  • I have several files I'm pulling, I just posted that one... Not trying to confuse. I don't have function names mixed up. No worries there. Just trying to get the reload to work. – Snarky_one Jul 16 '18 at 20:50
  • Okay, and does that poll now? (Also, no need for the `setTimeout` in the bottom bit of code I don't think...) – user184994 Jul 16 '18 at 20:51
  • I have no idea. I might not have explained well enough. Let me try this again. The top section pulls text from the file. I don't see any errors, it does pull it initially but whether it updates or not I don't know... due to the fact that the text is then read and run through the next script (the bottom segment), and then displayed. What I see on my screen is the initial run of it, I don't see it update and I assume it's because while it pulls the text, it's not applying the 2nd part again. – Snarky_one Jul 16 '18 at 20:54
  • Ah, in that case you do need the setTimeout, but you need to move it inside the ledload function – user184994 Jul 16 '18 at 20:56
  • I'm very unsure where you mean inside the function. – Snarky_one Jul 16 '18 at 20:58
  • I tried placing it here $('.canvasld, .crl').leddisplay($.extend(options, {pixelSize: 3})); setTimeout(ledload,5000); }); It's not erroring, but it's not updating on text file change either. – Snarky_one Jul 16 '18 at 21:03
  • I've added an example below of what I meant, give that a try – user184994 Jul 16 '18 at 21:05

3 Answers3

0

First I'd pull ledload() out of the $window.ready(). This way, it can be referenced by it's name. Also, I'm pretty sure you don't need the $(...) wrapper for the follow_pull() function...

function ledload() {
    var options = {
        pixelSize: 5, 
        stepDelay: 62, 
        horizontalPixelsCount:650,
        verticalPixelsCount:5,
        pixelRatio: 0.8,
        pathToPixelImage: 'ticker/pixel.png',
        backgroundColor: '#000',
        disabledPixelColor : '#020202',
        enabledPixelColor: '#ff522b'
    };
    $('.canvasld, .crl').leddisplay($.extend(options, {pixelSize: 3}));
}

function follow_pull() {    
    $.ajax({
        url : "most_recent_follower.txt",
        dataType: "text",
        success : function (data) {
            $("#follow").append(data).serialize();
            ledload();

            setTimeout(function(){
                follow_pull();
            }, 10000);
        }
    });
}; 

follow_pull() calls ledload() when it successfully gets the data. Then it sets up a 10 second delay before it does it all over again.

If you still wanted ledload to run on $(window).ready(), you can add this line as well:

$(window).ready(ledload);

P.S. I don't see what .serialize() is doing...Is that supposed to get passed into ledload()?

Partik
  • 808
  • 9
  • 15
  • follow_pull isn't the only function that needs to complete before ledload is called. I had originally copied bits of code from other sources to splice this together. I just removed serialize and it doesn't seem to break it. – Snarky_one Jul 16 '18 at 23:21
0

You're on the right track, but you need to move the setTimeouts slightly, like so:

$(function follow_pull() {
    $.ajax({
        url : "most_recent_follower.txt",
        dataType: "text",
        success : function (data) {
            $("#follow").append(data).serialize();
            setTimeout(fuction(){
                follow_pull()
            }, 10000);
        },
    });
}); 

This way, once the data has successfully loaded, it will call follow_pull again after 10 seconds.

For the other bit, you need to move it like so:

$(window).ready(function ledload() {
    var options = {
        pixelSize: 5,
        stepDelay: 62,
        horizontalPixelsCount: 650,
        verticalPixelsCount: 5,
        pixelRatio: 0.8,
        pathToPixelImage: 'ticker/pixel.png',
        backgroundColor: '#000',
        disabledPixelColor: '#020202',
        enabledPixelColor: '#ff522b'
    };
    $('.canvasld, .crl').leddisplay($.extend(options, {
        pixelSize: 3
    }));

    setTimeout(fuction() {
        ledload()
    }, 10000);
});

The AJAX ready function only takes one argument, but you were passing the setTimeout as a second argument, so it was being ignored.

user184994
  • 17,791
  • 1
  • 46
  • 52
  • The top segment has no errors. But once I apply the bottom it no longer executes the styling script. Would seeing the entire file help? Or the Style Script? – Snarky_one Jul 16 '18 at 21:13
  • 1
    @Snarky_one I'm not sure what the styling script is, so I guess seeing that would help. – Partik Jul 16 '18 at 21:16
  • Yep, all the relevant code would be good please, although what would be even better would be a jsfiddle or a StackOverflow snippet that reproduces the problem – user184994 Jul 16 '18 at 21:18
  • Here's the link to the file I'm using. [link](https://pste.eu/p/k6xB.html) And this is the leddisplay,js file [link](https://pste.eu/p/akZj.html) – Snarky_one Jul 16 '18 at 21:19
  • Just for clarity, I've been messing with the subscriber file first. If that one works I can repeat with the others. – Snarky_one Jul 16 '18 at 21:25
  • @Snarky_one Not sure if you copied and pasted the above code, but the functions have typos, not sure if that gave you a Syntax Error and stopped the execution. Twice it says 'fuction' instead of 'function'. – Partik Jul 16 '18 at 21:25
  • I did copy/paste the code. This version runs, doesn't error but doesn't refresh the changes if I change the text file either. [link](https://pste.eu/p/SJfN.html) – Snarky_one Jul 16 '18 at 21:29
  • I fixed where it said fuction. And replaced it with the Timeout as you showed. It doesn't error, but no refresh. – Snarky_one Jul 16 '18 at 21:36
  • @Snarky_one Can you a) Add a `console.log` inside the `success` function so that we can see when its called, and b) Check the developer tools network tab so see if the request is being made again after the 10 seconds? – user184994 Jul 16 '18 at 21:40
  • I can but I'm not executing this through a browser, so i have no way to view the log. If I try in a browser it shows cross origin requests and nothing more. – Snarky_one Jul 16 '18 at 21:47
  • Am I being confusing or difficult? I'm trying to follow, but I don't know how else to make this work. Would uploading it to a webserver for the time being make this possible? – Snarky_one Jul 16 '18 at 22:18
  • Okay I tried that, uploaded it to a webspace and it is polling in that it's pulling those text files. So that part is working. But I don't think the 2nd part is working where it applies the jquery.leddisplay.js and performs the last function. – Snarky_one Jul 16 '18 at 22:29
  • You can see it working here [link](http://burningl.com/ledtest/LED.html) – Snarky_one Jul 16 '18 at 22:31
  • @Snarky_one What are you expecting to see when that works? As far as I can see, none of those parameters are changing, so I'm not sure what you expect the behaviour to be... – user184994 Jul 16 '18 at 23:57
  • Well as you can see it pulls from the text files. What I'm hoping is that when the text in the files change, the ticker will update with the correct text. If it has to complete a rotation or something first, that's fine. What I was doing before was a standard meta refresh but that blanks out the page and looks horrible. I thought this would be much better. – Snarky_one Jul 17 '18 at 00:08
  • Yeah I have no clue how to accomplish my goal at this point. I thought it'd be easy as saying to have the files be refetched, and then have the script run again. Clearly I'm missing something. I just don't know JS well enough, or AJAX. If you have any other suggestions I'm open to trying them. You should be able to see all the code of both files. – Snarky_one Jul 17 '18 at 00:38
  • Is it simply not going to work with how the code is done in the jquery.leddisplay.js? Can I not repeat that function? – Snarky_one Jul 17 '18 at 01:39
  • @Snarky_one The problem is, there's no way to update the text as far as I can see. As soon as the ticker initialises, it removes the inner HTML, so `$("#follow")` no longer matches anything. You could always try and modify the source of `leddisplay.js` to try and expose the `init` function of `divMethods` – user184994 Jul 17 '18 at 03:52
  • I'm fine with doing it, but i have no idea how – Snarky_one Jul 17 '18 at 06:46
  • I didn't realize it removed the html. I'm open to any suggestions you might have. From changing something to rewriting all the code. Nothing has to stay, as long as the effect works and loads appropriately – Snarky_one Jul 17 '18 at 06:50
  • @Snarky_one I posted a [new answer](https://stackoverflow.com/questions/51369629/ajax-reload-interval-polling-assistance/51385982#51385982) based on the info you provided. Please see if that works for you. – Partik Jul 17 '18 at 16:13
0

So reviewing what you provided in your comment, I found a way to get it working. First, is the html below. Here are the differences:

  • I created a new element <div class="led"></div>. I also gave the .crl css to that element, and instead made .crl have display: none. This is because the .leddisplay function takes the element and replaces it with it's own HTML to render the LEDs. So you need to keep the div you are using to store your info separate from the div you are using to render it. (I would recommend just using JS variables to store that info, but I'm not trying to rewrite your code, just trying to get it working.)
  • But then how do you get the text into the LED display? With .leddisplay you can input the text you want as the second parameter of the function. You can see how I did that in postload().
  • To update your info, you were using append(). This adds to the divs, but you want to update them, so I replaced every .append() with .text() to replace the text rather than add on to it.
  • Finally, the heart of the solution. The leddisplay plugin doesn't have a way to update the led. So you have to 'destroy' it, and then rerun it, as I have done in the setTimeout() of postload(). But by itself, starts the scrolling all over again every 10 seconds. So what I do is track the current position, then after rerunning it, I resume the scrolling from there. However to make that work, I needed to update the plugin code. Below the HTML is the explanation for that.

HTML:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<style>
.led {
padding-top: 2px;
padding-bottom: 2px;
background-color: #444; 
}
.crl {
    display: none;
}
</style>
<div class="top-bar"></div>
<div class="crl">Newest Subscriber - <span id="sub"></span>    
LAST DONATION - <span id="donation"></span>    
LATEST BITS - <span id="bits"></span>    
rECENT FOLLOWEr - <span id="follow"></span>    
Sub Goal - <span id="subgoal"></span> / 80</div>
<div class="led"></div>
<div class="bottom-bar"></div>

<script type="text/javascript">
$.ajaxSetup({
  async: false,
  cache: false
 });
$(function follow_pull() {
        $.ajax({
            url : "most_recent_follower.txt",
            dataType: "text",
            success : function (data) {
            console.log(data);          
                $("#follow").text(data);
            setTimeout(function(){
            follow_pull()
            }, 10000);
        },
    });
}); 
$(function donator_pull() { 
        $.ajax({
            url : "most_recent_donator.txt",
            dataType: "text",
            success : function (data) {
            console.log(data);          
                $("#donation").text(data);
            setTimeout(function(){
            donator_pull()
            }, 10000);
        },
    });
}); 
$(function cheerer_pull() {     
        $.ajax({
            url : "most_recent_cheerer.txt",
            dataType: "text",
            success : function (data) {
            console.log(data);          
                $("#bits").text(data);
            setTimeout(function(){
            cheerer_pull()
            }, 10000);
        },
    });
}); 
$(function subscriber_pull() {  
        $.ajax({
            url : "most_recent_subscriber.txt",
            dataType: "text",
            success : function (data) {
            console.log(data);          
            $("#sub").text(data);
            setTimeout(function(){
            subscriber_pull()
            }, 10000);
        },
    });
}); 
$(function count_pull() {       
        $.ajax({
            url : "total_subscriber_count.txt",
            dataType: "text",
            success : function (data) {
            console.log(data);
                $("#subgoal").text(data);
           setTimeout(function(){
            count_pull()
            }, 10000);
        },
    });
}); 
$(function ledload() {
$.getScript( "ticker/jquery.leddisplay.js", function( data, textStatus, jqxhr ) {
  console.log( data ); // Data returned
  console.log( textStatus ); // Success
  console.log( jqxhr.status ); // 200
  console.log( "Load was performed." );
    }); 
});
$(function postload() {
    var options = {
            pixelSize: 5, 
            stepDelay: 62, 
            horizontalPixelsCount:650,
            verticalPixelsCount:5,
            pixelRatio: 0.8,
            pathToPixelImage: 'ticker/pixel.png',
            backgroundColor: '#000',
            disabledPixelColor : '#020202',
            enabledPixelColor: '#ff522b'
        };
        $(".led").leddisplay($.extend(options, {
            pixelSize: 3
        }), $('.crl').text());

        setTimeout(function () {
            //get the current position
            var x = $(".led").leddisplay('getCurrentPosition')

            //destroy the led setup
            $('.led').leddisplay('destroy');

            //create it again
            postload();

            //set the position to where it left off at
            $(".led").leddisplay('setCurrentPosition', x)
        }, 10000);
});
</script>

Inside the plugin, look for customMethods towards the bottom. I added 2 more methods to it: getCurrentPosition and setCurrentPosition, so it should look like this:

jquery.leddisplay.js, customMethods:

var customMethods = {
    init: function(){
        var _arg = arguments;
        return this.each(function() {
            var $this = $(this);
            if ($this.data('leddisplay'))
                return;

            $this.data('leddisplay', true);
            var methods = resolveMethods(this);
            methods.init.apply($this, _arg);
        });
    },
    destroy: function(){
        var _arg = arguments;
        return this.each(function() {
            var $this = $(this);
            if (!$this.data('leddisplay'))
                return;

            $this.data('leddisplay', null);
            var methods = resolveMethods(this);
            methods.destroy.apply($this, _arg);
        });
    },
    start: function(){

    },
    stop: function(){

    },
    getCurrentPosition: function(){
        return $(this).data('currentPosition');
    },
    setCurrentPosition: function(x){
        $(this).data('currentPosition', x);
    }
}

After you make these changes, it should work as expected.

Partik
  • 808
  • 9
  • 15
  • Going to test this now, will let you know shortly. – Snarky_one Jul 17 '18 at 16:46
  • This did it. It works perfectly. Even the little stutter it gives, doesn't bother me at all. Thank you so much!!! Now to go back and study all the methods. Thank you for the explanations, that helps a whole lot. I'm still new to Jquery/Ajax/Java programming. I would have done it in PHP, but since it's run locally and there's not a webserver available, this is perfect! – Snarky_one Jul 17 '18 at 16:58
  • I can tell you're new to it because you said "Java" instead of "Javascript" :) (two very different languages, but confusingly named...). But it's all good, gotta start somewhere, so I'm glad to help! The stutter happens because of the `async: false` part. It has to wait for all the pulls to finish before it continues rendering the led. There's a way to remove the stutter, but it would require several changes and some intermediate level "async" stuff to do it cleanly, so if it really doesn't bother you, I'd leave it as is. – Partik Jul 17 '18 at 18:15
  • For this particular instance, it's perfect (the theme is circa 1950's) so the stutter actually fits. But knowing that I could work out some stuff to make it smooth is promising. – Snarky_one Jul 18 '18 at 04:32