Created
May 5, 2015 06:33
-
-
Save zhang6464/45fadfe9b03320dd3003 to your computer and use it in GitHub Desktop.
open new window after click but determined by ajax result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<button class="btn">123</button> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.btn').on("click", function(){ | |
var ajax = new $.Deferred(); | |
$.ajax({ | |
url: '/', | |
success: function (data) { | |
if (data) { | |
ajax.resolve(); | |
} | |
} | |
}); | |
openNewWin('http://www.baidu.com', ajax); | |
}); | |
function openNewWin(url, ajax, waiting) { | |
var stack = [], current = 0; | |
waiting = waiting > 0 ? waiting : 5; | |
for (var i = 1; i < waiting; i++ ) { | |
stack.push(setTimeout(function() { | |
if (ajax.state() == "resolved") { | |
$.each(stack, function (key, val) { | |
console.log(val); | |
clearTimeout(val); | |
}); | |
window.open(url); | |
} else { | |
stack.shift(); | |
} | |
}, i * 1000)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment