$.ajax({ type: "POST", url: "post_computation_analogy", data: {"y":y,"x":x}, traditional: true, // async: false, success: function (rtn) { // alert("计算结果为:"+rtn); console.log(rtn) // data[0]=rtn[0] // data[1]=rtn[1] }, // result0=data[0]*input+data[1], });想要在Ajax之外访问rtn的返回,如何做
时间: 2024-01-27 07:05:23 浏览: 100
在Ajax之外访问Ajax返回的数据可以使用回调函数的方式。在Ajax请求中,可以将成功返回后的数据通过回调函数的形式传递给其他函数进行处理。例如:
```
function ajaxRequest(callback) {
$.ajax({
type: "POST",
url: "post_computation_analogy",
data: {"y":y,"x":x},
traditional: true,
success: function (rtn) {
callback(rtn);
}
});
}
// 调用ajaxRequest函数,并将回调函数作为参数传递
ajaxRequest(function(rtn) {
console.log(rtn); // 在回调函数中访问返回的数据
// 在这里对返回的数据进行处理
});
```
在这个例子中,`ajaxRequest`函数接收一个回调函数作为参数,并在Ajax请求成功后将返回的数据通过回调函数传递出去。调用`ajaxRequest`函数时,将需要执行的函数作为参数传递给它。这样,在Ajax请求成功时,就可以通过回调函数访问返回的数据并进行处理。
相关问题
calculate(){ const limit=this.tableData.length; let y=[]; let x=[]; for(let i=0;i<=limit-1;i++){ y.push(parseFloat(this.tableData[i].flux)); x.push(parseFloat(this.tableData[i].area)); } $.ajax({ type: "POST", url: "post_computation_analogy", data: {"y":y,"x":x}, traditional: true, // async: false, success: function (rtn) { const result0 = rtn[0] * parseFloat(this.input) + rtn[1]; }, flux0.innerHTML="涌水量为"+this.result0+"涌水量单位" }); }这段代码最后一行无法正常运行是什么问题
这段代码最后一行的语法是有问题的,应该将最后一行放在 `success` 回调函数中。修改后的代码如下:
```
calculate() {
const limit = this.tableData.length;
let y = [];
let x = [];
for (let i = 0; i <= limit - 1; i++) {
y.push(parseFloat(this.tableData[i].flux));
x.push(parseFloat(this.tableData[i].area));
}
$.ajax({
type: "POST",
url: "post_computation_analogy",
data: { "y": y, "x": x },
traditional: true,
success: (rtn) => {
const result0 = rtn[0] * parseFloat(this.input) + rtn[1];
flux0.innerHTML = "涌水量为" + result0 + "涌水量单位";
}
});
}
```
将最后一行移到 `success` 回调函数中,并且使用箭头函数来确保 `this` 指向正确。另外,将 `flux0.innerHTML` 也放在回调函数中,以确保在异步请求成功后再执行这个操作。
没有GPU,优化程序class point_cloud_generator(): def init(self, rgb_file, depth_file, save_ply, camera_intrinsics=[312.486, 243.928, 382.363, 382.363]): self.rgb_file = rgb_file self.depth_file = depth_file self.save_ply = save_ply self.rgb = cv2.imread(rgb_file) self.depth = cv2.imread(self.depth_file, -1) print("your depth image shape is:", self.depth.shape) self.width = self.rgb.shape[1] self.height = self.rgb.shape[0] self.camera_intrinsics = camera_intrinsics self.depth_scale = 1000 def compute(self): t1 = time.time() depth = np.asarray(self.depth, dtype=np.uint16).T self.Z = depth / self.depth_scale fx, fy, cx, cy = self.camera_intrinsics X = np.zeros((self.width, self.height)) Y = np.zeros((self.width, self.height)) for i in range(self.width): X[i, :] = np.full(X.shape[1], i) self.X = ((X - cx / 2) * self.Z) / fx for i in range(self.height): Y[:, i] = np.full(Y.shape[0], i) self.Y = ((Y - cy / 2) * self.Z) / fy data_ply = np.zeros((6, self.width * self.height)) data_ply[0] = self.X.T.reshape(-1)[:self.width * self.height] data_ply[1] = -self.Y.T.reshape(-1)[:self.width * self.height] data_ply[2] = -self.Z.T.reshape(-1)[:self.width * self.height] img = np.array(self.rgb, dtype=np.uint8) data_ply[3] = img[:, :, 0:1].reshape(-1)[:self.width * self.height] data_ply[4] = img[:, :, 1:2].reshape(-1)[:self.width * self.height] data_ply[5] = img[:, :, 2:3].reshape(-1)[:self.width * self.height] self.data_ply = data_ply t2 = time.time() print('calcualte 3d point cloud Done.', t2 - t1) def write_ply(self): start = time.time() float_formatter = lambda x: "%.4f" % x points = [] for i in self.data_ply
It that the code is generating a point cloud from an RGB-D image pair. Since you mentioned that you do not have a GPU, one possible optimization could be to use the `numba` library to speed up the computation. Here is how you can modify the code to use `numba`:
1. Import the `numba` library by adding the following line at the top of your code:
```python
import numba
```
2. Add the `@numba.jit(nopython=True)` decorator to the `compute` method to enable `numba` JIT compilation:
```python
@numba.jit(nopython=True)
def compute(self):
# rest of the code
```
3. Modify the `for` loops in the `compute` method to use `numba`'s `prange` function to parallelize the computation across multiple CPU cores:
```python
for i in numba.prange(self.width):
# rest of the code
for i in numba.prange(self.height):
# rest of the code
```
4. Add the `@numba.jit(nopython=True)` decorator to the `write_ply` method as well, since it is also computationally intensive:
```python
@numba.jit(nopython=True)
def write_ply(self):
# rest of the code
```
With these modifications, the `compute` and `write_ply` methods should run faster. However, keep in mind that `numba`'s JIT compilation can take some time during the first function call, so subsequent calls should be faster. Also, note that `numba`'s `prange` function only works with integer indices, so you may need to convert floating-point indices to integers before using them in the loops.
阅读全文