F
F
fandorin_official2018-01-14 10:11:27
JavaScript
fandorin_official, 2018-01-14 10:11:27

How to fix script stop?

All good.
There is a userscript (some variables and husks, not very informative, I threw away):

(function (window, undefined) {  // [2] нормализуем window
    var w;
    if (typeof unsafeWindow != undefined) {
        w = unsafeWindow;
    } else {
        w = window;
    }
    if (w.self != w.top) {
        return;
    }

    function start() {
        var read = getread();
        checkResult(read);
    }

    function checkResult(last_read) {
        setTimeout(function() {
            if (last_read < getread()) {
                
            } else if (last_read > getread()) {

            } else {
                checkResult(last_read);
            }
        }, 500);
    }

    function getread() {
        var read_text = $('.read_select .chosen-single span').text();
        var regex = /[+-]?\d+(\.\d+)?/g;
        var floats = read_text.match(regex).map(function(v) {
            return parseFloat(v).toFixed(8);
        });
        return floats[0];
    }
    
    function minimum_rate() {
    }

    function callnetwork() {
    }
})(window);

This script "parses" the online chat of one site. Sometimes on the checkResult function, the script cannot get a response from the site and it fulfills the third condition and restarts the checkResult function again. And so it can be many times. Sometimes, after another dozen restarts, the first or second condition is triggered, and then everything is fine. But sometimes, after many dozens of executions of the third condition, the script "gets tired" and stops being executed.
How can the script be improved so that it does not break when there is some delay in the response from the site?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-01-14
@fandorin_official

Sometimes on the checkResult function, the script cannot get a response from the site and it fulfills the third condition and restarts the checkResult function again.

Nonsense. There is no answer. The code is synchronous (apart from setTimeout) and without requests. Check your terms. Just output in the body of the setTimeout callback to the console:
console.log(last_read);
console.log(getRead());
console.log(last_read < getread());
console.log(last_read > getread());

It's probably not what you had in mind.
And your script does not "get tired", but completes its execution. The shortest path to complete is: start() -> getRead() -> checkResult() -> getRead()
I don't know what you're thinking, but it's enough for the script not to stop after the conditions to execute checkResult :
function checkResult(last_read) {
        setTimeout(function() {
            if (last_read < getread()) {
                
            } else if (last_read > getread()) {

            } else {
                checkResult(last_read);
            }

            checkResult(getRead());
        }, 500);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question