我正在使用odoo 10,它使用的是ribsjs。我在使用带有ajax.jsonRpc的setInterval函数来反复调用rest api并接收数字权重值。
我当前的代码是:
var loopid = setInterval(function () {
ajax.jsonRpc('/pos/weight/bridge','process_weight_data',{'input_data':0}).then(function (data) {
var weightage_bridge=0;
weightage_bridge=data['ibos_weight'];
document.getElementById('wrdr').value = weightage_bridge;
console.log('Ajax Json RPC Weight POPUP: ' + weightage_bridge);
clearInterval(loopid);
},function (err) {
console.log('dave err: ' + err);
clearInterval(loopid);
});
},200)
我的要求是改用setTimeout。如何使用setTimeout重写上面的代码,如下例所示,但使用ajax.jsonRpc():
var interval = 1000; // 1000 = 1 second, 3000 = 3 seconds
function doAjax() {
$.ajax({
type: 'POST',
url: 'increment.php',
data: $(this).serialize(),
dataType: 'json',
success: function (data) {
$('#hidden').val(data);// first set the value
},
complete: function (data) {
// Schedule the next
setTimeout(doAjax, interval);
}
});
}
我的完整javascript文件如下:
odoo.define('ibos_pos_weighbridge.weigh_bridge_popups',function (require) {
"use strict";
var PosBaseWidget = require('point_of_sale.BaseWidget');
var gui = require('point_of_sale.gui');
var _t = require('web.core')._t;
var ajax = require('web.ajax');
var IbosPopupWidget = PosBaseWidget.extend({
template: 'PopupWidget',
init: function (parent, args) {
this._super(parent, args);
this.options = {};
},
events: {
'click .button.cancel': 'click_cancel',
'click .button.confirm': 'click_confirm',
'click .selection-item': 'click_item',
'click .input-button': 'click_numpad',
'click .mode-button': 'click_numpad',
},
// show the popup !
show: function (options) {
if (this.$el) {
this.$el.removeClass('oe_hidden');
}
if (typeof options === 'string') {
this.options = {title: options};
} else {
this.options = options || {};
}
this.renderElement();
// popups block the barcode reader ...
if (this.pos.barcode_reader) {
this.pos.barcode_reader.save_callbacks();
this.pos.barcode_reader.reset_action_callbacks();
}
},
// called before hide, when a popup is closed.
// extend this if you want a custom action when the
// popup is closed.
close: function () {
if (this.pos.barcode_reader) {
this.pos.barcode_reader.restore_callbacks();
}
},
// hides the popup. keep in mind that this is called in
// the initialization pass of the pos instantiation,
// so you don't want to do anything fancy in here
hide: function () {
if (this.$el) {
this.$el.addClass('oe_hidden');
}
},
// what happens when we click cancel
// ( it should close the popup and do nothing )
click_cancel: function () {
this.gui.close_popup();
if (this.options.cancel) {
this.options.cancel.call(this);
}
},
// what happens when we confirm the action
click_confirm: function () {
this.gui.close_popup();
if (this.options.confirm) {
this.options.confirm.call(this);
}
},
// Since Widget does not support extending the events declaration
// we declared them all in the top class.
click_item: function () {
},
click_numad: function () {
},
});
gui.define_popup({name: 'alert', widget: IbosPopupWidget});
var IbosWeightReaderPopupWidget = IbosPopupWidget.extend({
template: 'IbosWeightReaderPopupWidget',
show: function (options) {
options = options || {};
this._super(options);
this.renderElement();
this.$('input,textarea').attr('disabled',true);
var loopid = setInterval(function () {
ajax.jsonRpc('/pos/weight/bridge','process_weight_data',{'input_data':0}).then(function (data) {
var weightage_bridge=0;
weightage_bridge=data['ibos_weight'];
document.getElementById('wrdr').value = weightage_bridge;
console.log('Ajax Json RPC Weight POPUP: ' + weightage_bridge);
clearInterval(loopid);
},function (err) {
console.log('dave err: ' + err);
clearInterval(loopid);
});
},200)
this.$('input,textarea').focus();
},
click_confirm: function () {
var value = this.$('input,textarea').val();
this.gui.close_popup();
if (this.options.confirm) {
this.options.confirm.call(this, value);
}
},
});
gui.define_popup({name: 'ibosweightreader', widget: IbosWeightReaderPopupWidget});
});
请使用ajax.jsonRpc (包括 complete:function()
),通过setTimeout而不是setInterval协助实现 loopid()函数