从事件处理程序将值从javascript返回到父函数 [英] Return value from event handler into parent function from javascript

查看:38
本文介绍了从事件处理程序将值从javascript返回到父函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在事件处理程序返回一个值,该函数首先启动事件,仅返回将仅在事件处理程序中接收的值.我相信应该有一种简便的方法来完成此任务或从不同角度解决问题.

I need to have a value returned by and event handler into the function that initiates the event in the first place, just to return the value that will be received only in the event handler. I believe there should be an easy way to accomplish this or to attack the problem from a different angle.

我真正需要做的是:

  1. 创建一个新的img DOM对象以将dataURL馈入(dataURL来自canvas.toDataURL())
  2. img.onload将让我返回dataURL

如果使用jQuery可以有所帮助,但我不一定要使用它.

I'm using jQuery if that helps somehow but don't have to of course.

据我了解,我不能立即依赖img来获取数据,并且使用超时来等待事件触发并使用一些全局变量感觉很愚蠢,尤其是因为其中一些可以并行处理.

In my understanding I can't rely on the img to have the data immediately, and it feels stupid to use timeouts to wait for the event to fire and use some global variable, especially since a few might be processed in parallel.

要获得更多背景,这都是为了在通过HTML5引擎(多)上传图像之前调整图像的大小.

For even more background, this is all for resizing images before (multi-)uploading them with HTML5 engine.

甚至感谢您阅读,当然还有任何提示:)

Thanks even for reading, and for any tips of course :)

谢谢,伊戈尔(Igor)

Thanks, Igor

推荐答案

从事件处理程序中返回值没有意义.事件在事件发生时发生,并且处理程序在那时由浏览器调用.因此,没有代码期望任何返回值.无处可去.

It doesn't make sense to return values from an event handler. Events happen when they happen, and the handler is called by the browser at that time. Thus, there's no code expecting any return value; there's nowhere for it to go.

相反,只需在事件处理程序中执行 所需的任何操作即可.

Instead, simply do whatever it is you need to do inside the event handler.

这篇关于从事件处理程序将值从javascript返回到父函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆