jQuery: The Write Less, Do More JavaScript Library

 « Return to Thread: aborting a $.getScript() request

Re: aborting a $.getScript() request

by Maujor :: Rate this Message:

| View in Thread


>However  you could set a flag in the success/callback function. After
>the getScript() call you could use a setTimeout to test for that flag
>after a certain time. If the flag isn't set then the load didn't work
>or took too long.
>Karl Rudd
------------------------------------------------------------------------------------
Hi  Karl
Thanks a lot for your advise.

The script is working in Firefox and Opera but fails in IE6/IE7 and Safari
on Windows.

Test case at: http://jsbin.com/atacu
Edit at: http://jsbin.com/atacu/edit

Any thoughts?

var iconLoading = $('<img src="icon.gif" />');
 var pluginLoaded = $('<span>Plugin loaded</span>');
 var requestScript = function() {
 $('#carrega').unbind('click'); // prevent popup the alerbox on 2nd click
when request fails
 $(iconLoading).insertAfter('#carrega');

//  Begins Karl suggestion
 var someTime = setTimeout( function() {
  var flagTime = true;
  if(flagTime) {
  $(iconLoading).hide();
  alert("Error message.")
  }
 }, 30000 )  // Wait 30s for request success
 //  End Karl suggestion

$.getScript('http://www.domain.com/scrip.js', function() {
 clearTimeout(someTime); // Karl suggestion
 $(iconLoading).hide();
 $(pluginLoaded).insertAfter('#carrega')
 .css({background: '#ffc', padding:'3px 5px'})
 .fadeOut(3000);
 // do something using the just loaded plugin
 });
 });
 };
  $('#carrega').bind('click', requestScript);
})
--------------------------------------------------------------------------------------

Thanks
MaurĂ­cio.


MaurĂ­cio Samy Silva

 « Return to Thread: aborting a $.getScript() request