Create a jQuery user interface using setTimeout ()

I am trying to close my jQuery UI interface after 5 seconds, but the code below does nothing, no suggestions? I tested it with alert("hellow") and it worked fine, but the code below does not work.

 success: function(data) { $(data).dialog({ modal: true, width: 900, height: 600, resizable: false, title: thetitle, draggable: false, open: function(event, ui) { setTimeout('$(this).dialog("close");', 5000); } }); 

Why doesn't this close my dialog after 5 seconds? He does not do anything.

+4
source share
3 answers

You need to pass setTimeout valid function, not a string.

 setTimeout(function() { $(data).dialog("close"); }, 5000); 

When you pass the string, the code is eval 'd, which I'm sure sets this for the global object (so $(this).dialog will never work).

Note that this will not work with the above method (since again this is a global object at this point), but it is still considered much better than passing a string to setTimeout .

+6
source

What about jquery.delay ()?

 success: function(data) { $(data).dialog({ modal: true, width: 900, height: 600, resizable: false, title: thetitle, draggable: false, open: function(event, ui) { $(this).dialog("close").delay(5000); } }); 
+1
source
 var sT = setTimeout('$(this).dialog("close");', 5000); 
0
source

Source: https://habr.com/ru/post/1386882/


All Articles