为什么Settimeout没有拉出更新的图片网址?

时间:2013-07-01 21:54:25

标签: javascript jquery ruby

我正在使用Aviary在线编辑图像。通常我有一个本地文件,我点击,编辑和保存。

保存后,图片的src路径会从本地“uploadedimage.jpg”更改为长的临时网址,如:

http://featherfiles.aviary.com/2013-07-01/kvs78lgyil6sxlc5/69318ad57a1d490d8215cc1097cf6c32.png

我正在尝试让JavaScript在编辑完成后获取更新的URL。我尝试设置“settimeout”参数,但它仍然抓住图像的原始本地URL。

<!-- Load Feather code -->
<script type="text/javascript" src="http://feather.aviary.com/js/feather.js"></script>

<!-- Instantiate Feather -->

<script type='text/javascript'>
 var featherEditor = new Aviary.Feather({
   apiKey: 'apikey',
   apiVersion: 2,
   tools: 'all',
   appendTo: '',
   onSave: function (imageID, newURL) {
       var img = document.getElementById(imageID);
       img.src = newURL;
setTimeout(function(){fillform()}, 5000);
 },
   onError: function(errorObj) {
       alert(errorObj.message);
   }
   });
   function launchEditor(id) {
   featherEditor.launch({
       image: id
   });
  return false;
   }
</script>


<div id='injection_site'></div>
<%= form_for @post, :html => { :class => 'form-horizontal' } do |f| %>
<img id='image1' <%= image_tag @post.productimage_url.to_s %>
<% end %>
<!-- Add an edit button, passing the HTML id of the image and the public URL of the image -->
<p><input type='image' src='http://images.aviary.com/images/edit-photo.png' value='Edit photo' onclick="return launchEditor('image1');" /></p>



<script type="text/javascript" language="JavaScript">
var bob = document.getElementById('image1').src;
var fillform = function ()
{
setTimeout(function(){document.getElementById("textbox1").value=bob}, 3000);
}
</script>

myformfield: <input type="text" name="name_textbox" id="textbox1" />

1 个答案:

答案 0 :(得分:3)

这很可能是因为:

var bob = document.getElementById('image1').src;

正在制作字符串的副本。因此,当你运行:

document.getElementById("textbox1").value=bob

您正在使用最初定义时的字符串。您可能希望将回调更改为:

setTimeout(function()
   {
      var bob = document.getElementById('image1').src; // Get current value
      document.getElementById("textbox1").value=bob
   }, 3000);

然后你也可以摆脱全球 bob 变量。

另一种解决方法是更改​​onSave回调:

img.src = newURL;

为:

bob = img.src = newURL; // Update bob