javascript - How to update the right span? -


On my forum-based website, whenever I have a link under each post to report spam or abuse This link is clicked, then a web service is called on the server, when the call returns, the link (see code given below) is updated in the same way as the 'posted report'. Or if an error occurs, then report this 'post' An error occurred while registering, this is the JavaScript code:

  & lt; Script src = "/ js / MicrosoftAjax.js" type = "text / javascript" language = "javascript" & gt; & Lt; / Script & gt; & Lt; Script type = "text / javascript" language = "javascript" & gt; Var spanToUpdate; Function Report Post (UpdateSpain, PostID) {If (Confirm ("Are you sure you want to report this post as spam or abuse?") {SpanToUpdate = updateSpan; Var proxy = SiteWS.ReportPost (postID, onReportPostSuccess, onReportPostFailure); }} Function on the postpostsuit (sender, e) {spanToUpdate.innerHTML = "post report"; } ReportPostFailure on function (sender, e) {spanToUpdate.innerHTML = "An error occurred while reporting the post;" } & Lt; / Script & gt;  

and this is a reporting link:

   

As you can see, I have a variable, I use spenchadet, which has to capture the context of Span, which has a reporting link, which means that the user reports another post, call retrievers (Before clicking on another reporting link), the last posting period will be updated twice and will not be updated in the past, is there any alternative solution for this?

Many thanks

You can use anonymous functions and closures for it.

 function ReportPost (updateSpan, postID) {If (confirm ("Are you sure you want to report this post as spam or abuse?"}} {Var proxy = SiteWS.ReportPost (PostID, function (sender, e) {updateSpan.innerHTML = "post report"}, function (sender, e) {updateSpan.innerHTML = "An error occurred while reporting the post}}); }} 

Edit:

Hmm .. Now thinking, when the anonymous method is called, then the SAPA will mention the same period? - Valid Isa
Yes, this is Closer's magic. Try this small example:
   & lt; Script & gt; Function fu () {times (1, 100); Bar (2, 150); Bar (3, 200); Bar (4, 250); Bar (5,300); Document.getElementById ("div1"). InnerHTML + "foo" is used. "; Return;} function bar (val, timeout) {window.setTimeout (function () {document.getElementById (" div1 "). InnerHTML +" "+ val +"}, timeout);} & lt; / Script>  gt; body & gt; & gt; button onclick = "foo ()" & gt; & lt; / button & gt;; & lt; div id = "Div1"> 
You will see that the anonymous function is called every time, when the bar () was called , Then "own" valve has been preserved from time / reference.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -