/** 模拟鼠标移动 改方法来自https://blog.csdn.net/Wuzihui___/article/details/79952068 * @param id * @param clientX 相对窗口横坐标 * @param clientY 相对窗口纵坐标 * @param distance 滑动距离 */ function dragandDrop(btn_hk, clientX, clientY, distance) { var elem = btn_hk, k = 0, interval; iME(elem, "mousedown", 0, 0, clientX, clientY); let waitTime = Math.floor(Math.random() * (0.005 * 1000 - 0.09 * 1000) + 0.09 * 1000) interval = setInterval(function () { k++; iter(k); if (k === distance) { clearInterval(interval); iME(elem, "mouseup", clientX + k, clientY, 220 + k, 400); } }, waitTime); function iter(y) { iME(elem, "mousemove", clientX + y, clientY, clientX + y, clientY); } function iME(obj, event, screenXArg, screenYArg, clientXArg, clientYArg) { var mousemove = document.createEvent("MouseEvent"); mousemove.initMouseEvent(event, true, true, unsafeWindow, 0, screenXArg, screenYArg, clientXArg, clientYArg, 0, 0, 0, 0, 0, null); obj.dispatchEvent(mousemove); } }优化这段代码
时间: 2023-03-20 21:01:11 浏览: 95
绑定halcon显示控件,可实现ROI交互,用于机器视觉领域.zip
很高兴为您解答,这段代码可以通过改进其中的参数,使其运行更加高效。例如,可以调整waitTime变量,以更好地满足您的需求;也可以减少对document.createEvent函数的调用,以节省资源;还可以通过将interval和iter函数抽取出来,以便更好地复用,提高代码的可读性。
阅读全文