QML中的XMLHttpRequest超时



如何使QML中的XMLHttpRequest超时?我有以下代码,但它不会超时。似乎没有实现超时功能!?有其他方法可以实现超时吗?

var http = new XMLHttpRequest();
http.open("POST", "http://localhost/test.xml", true);
http.setRequestHeader('Content-type', 'application/json; charset=utf-8')
http.timeout = 4000;
http.ontimeout = function () {
    console.log("timed out")
}
http.onreadystatechange = function() {
    if (http.readyState === XMLHttpRequest.DONE) {
        // Do something
    }
}
http.send(JSON.stringify(data));

编辑:代码不在qml中,而是在js文件中。它不会进入qml文件,因为它是模型(MVC)的一部分。

看起来QML XMLHttpRequest不支持timeout功能。这是支持的功能/属性子集列表:

http://qt-project.org/doc/qt-5/qtqml-javascript-qmlglobalobject.html#xmlhttprequest

但是您可以使用QML Timer项来实现此目的,例如:

import QtQuick 2.3
Item {
    id: root
    width: 600
    height: 400
    Text {
        anchors.fill: parent
        id: response
        text: "Click me"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        MouseArea {
            anchors.fill: parent
            onClicked: {
                response.text = "Loading...";
                var req = new XMLHttpRequest();
                var timer = Qt.createQmlObject("import QtQuick 2.3; Timer {interval: 4000; repeat: false; running: true;}",root,"MyTimer");
                timer.triggered.connect(function(){
                    req.abort();
                    response.text = "timed out";
                });

                req.open("GET","http://google.com--",true); // correct me
                req.onreadystatechange = function() {
                    if (req.readyState === XMLHttpRequest.DONE && req.status == 200) {
                        response.text = req.getAllResponseHeaders();
                        timer.running = false;
                    }
                }
                req.send();
            }
        }
    }
}

一个全局的setTimeout()setInterval()用程序,用于对浏览器中的功能进行建模。

它可以用于间接地使XMLHttpRequest超时。或者在应用程序的其他地方谨慎使用。

timeoutId = util.timer.setTimeout(function() {}, 4500);
util.timer.clearInterval(timeoutId);

主QML代码段

使用以下代码,任何子级都可以访问app.util.timer命名空间下的方法(如果JS代码加载在主codeehind文件中,则访问util.timer[/em>命名空间)。

import "CodeBehind.js" as CodeBehind
id: appWindow
QtObject {
     id: app
     property var util: CodeBehind.util
}
Component.onCompleted: {
    CodeBehind.util.timer.init(appWindow);
}

CodeBehindJS代码段

var util = util || {};
Qt.include("qrc:/Util/Timer.js");

实用程序JS文件

var util = util || {};
/**
    Mimics the window.setTimeout() and window.setInterval() functionality
    found in browsers.
    @namespace timer
    @memberof util
*/
util.timer = new function() {
    var _timer;
    var _timerList = [];
    var _currentTime = 0;
    var _api = {};
    var _private = {};
    var _enums = {};

    /*************** Private ENUMS ***************/
    _enums.type = {
        timeout: 0,
        interval: 1
    };

    /*************** Public API Methods ***************/
    /**
        Mimics the window.setTimeout() functionality found in browsers.
        @param {function} callback
        @param {int} interval Milliseconds
        @public
        @memberof util.timer
    */
    _api.setTimeout = function(callback, interval) {
        var timer, id;
        id = parseInt(Math.random() * Date.now());
        timer = new Timer(id, callback, interval, _enums.type.timeout);
        _timerList.push({ id: id, timer: timer });
        return id;
    };

    /**
        Mimics the window.setInterval() functionality found in browsers.
        @param {function} callback
        @param {int} interval Milliseconds
        @public
        @memberof util.timer
    */
    _api.setInterval = function(callback, interval) {
        var timer, id;
        id = parseInt(Math.random() * Date.now());
        timer = new Timer(id, callback, interval, _enums.type.interval);
        _timerList.push({ id: id, timer: timer });
        return id;
    };

    /**
        Clears an interval or timout by the interval id that is returned
        when setTimeout() or setInterval() is called.
        @param {int} intervalId
        @public
        @memberof util.timer
    */
    _api.clearInterval = function(intervalId) {
        var idx, len, idxOfTimer;
        if (_timerList) {
            // Find the index of the timer
            len = _timerList.length;
            for (idx = 0; idx < len; idx++) {
                if (_timerList[idx].id === intervalId) {
                    idxOfTimer = idx;
                    break;
                }
            }
            // Remove the timer from the array
            _timerList.splice(idxOfTimer, 1);
        }
        // If: There are no more timers in the timer list
        // Then: Stop the QML timer element
        if (!_timerList || _timerList.length === 0) {
            _private.stop();
        }
    };

    /*************** Private Helper Methods ***************/
    _private.start = function() {
        _timer.start();
    };
    _private.stop = function() {
        _timer.stop();
        _currentTime = 0;
    };
    _private.runInterval = function() {
        var idx, len, tempList;
        // Increment the current timer
        _currentTime++;
        if (_timerList) {
            // Create a temp list of timers
            tempList = [];
            len = _timerList.length;
            for (idx = 0; idx < len; idx++) {
                tempList.push(_timerList[idx].timer);
            }
            // Trigger each method
            len = tempList.length;
            for (idx = 0; idx < len; idx++) {
                tempList[idx].trigger();
            }
        }
    };

    /*************** Objects ***************/
    /**
        A timer object contains a trigger to check and see if it needs
        to run the callback.
        @param {int} id
        @param {function} callback
        @param {int} interval Milliseconds
        @param {enum} type type.interval | type.timeout
        @public
        @memberof util.timer
    */
    var Timer = function(id, callback, interval, type) {
        var _obj = {};
        _obj.api = {};
        _obj.hasRun = false;
        _obj.endTime = _currentTime + interval;
        _obj.api.trigger = function() {
            if (_currentTime >= _obj.endTime && !_api.hasRun) {
                _obj.hasRun = true;
                callback();
                if (type === _enums.type.interval) {
                    _obj.endTime += interval;
                    _api.hasRun = false;
                }
                else {
                    _api.clearInterval(id);
                }
            }
        };
        _private.start();
        return _obj.api;
    };

    /*************** Initialize ***************/
    _api.init = function(appWindow) {
        _timer = Qt.createQmlObject("import QtQuick 2.3; Timer { interval: 1; repeat: true; running: false;}", appWindow, "timer");
        _timer.triggered.connect(_private.runInterval);
    };

    return _api;
};

相关内容

  • 没有找到相关文章

最新更新