html 推荐的例子。 jQuery推迟了
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了html 推荐的例子。 jQuery推迟了相关的知识,希望对你有一定的参考价值。
$.loadImage = function(url) {
// Define a "worker" function that should eventually resolve or reject the deferred object.
var loadImage = function(deferred) {
var image = new Image();
// Set up event handlers to know when the image has loaded
// or fails to load due to an error or abort.
image.onload = loaded;
image.onerror = errored; // URL returns 404, etc
image.onabort = errored; // IE may call this if user clicks "Stop"
// Setting the src property begins loading the image.
image.src = url;
function loaded() {
unbindEvents();
// Calling resolve means the image loaded sucessfully and is ready to use.
deferred.resolve(image);
}
function errored() {
unbindEvents();
// Calling reject means we failed to load the image (e.g. 404, server offline, etc).
deferred.reject(image);
}
function unbindEvents() {
// Ensures the event callbacks only get called once.
image.onload = null;
image.onerror = null;
image.onabort = null;
}
};
// Create the deferred object that will contain the loaded image.
// We don't want callers to have access to the resolve() and reject() methods,
// so convert to "read-only" by calling `promise()`.
return $.Deferred(loadImage).promise();
};
// example
$.loadImage = function(url) {
// Define a "worker" function that should eventually resolve or reject the deferred object.
var loadImage = function(deferred) {
var image = new Image();
// Set up event handlers to know when the image has loaded
// or fails to load due to an error or abort.
image.onload = loaded;
image.onerror = errored; // URL returns 404, etc
image.onabort = errored; // IE may call this if user clicks "Stop"
// Setting the src property begins loading the image.
image.src = url;
function loaded() {
unbindEvents();
// Calling resolve means the image loaded sucessfully and is ready to use.
deferred.resolve(image);
}
function errored() {
unbindEvents();
// Calling reject means we failed to load the image (e.g. 404, server offline, etc).
deferred.reject(image);
}
function unbindEvents() {
// Ensures the event callbacks only get called once.
image.onload = null;
image.onerror = null;
image.onabort = null;
}
};
// Create the deferred object that will contain the loaded image.
// We don't want callers to have access to the resolve() and reject() methods,
// so convert to "read-only" by calling `promise()`.
return $.Deferred(loadImage).promise();
};
function wait( timeout ) {
var deferred = $.Deferred();
setTimeout( deferred.resolve , timeout );
return deferred.promise();
}
wait(1000).done( function(){
console.log('Timeout fired!');
});
jQuery(document).ready(function($){
var promises = [
$.ajax('http://google.com'),
$.ajax('http://yahoo.com'),
$.ajax('http://nytimes.com')
];
$.when.apply($, promises).then(
function(result1, result2, result3) {
console.log('All URLs fetched.');
}
);
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="container"></div>
<script src="js/jquery.min.js"></script>
<script>
jQuery(document).ready(function($){
var d = $.Deferred();
var promise = d.promise();
promise.then(
function(value){
return 'Done_1 '+value;
},
function(value){
return 'Fail_1 '+value;
},
function(value){
console.log('Progress 1');
return 'Progress_1 '+value;
}
).then(
function(value){
$('#container').append('<p>Done_2 '+value+'</p>');
},
function(value){
$('#container').append('<p>Fail_2 '+value+'</p>');
},
function(value){
$('#container').append('<p>Progress_2 '+value+'</p>');
}
);
promise.progress(function(value){
$('#container').append('<p>PROGRESS '+value+'</p>');
});
promise.done(function(value){
$('#container').append('<p>DONE '+value+'</p>');
});
promise.always(function(value){
$('#container').append('<p>ALWAYS '+value+'</p>');
});
d.notify(10);
d.notify(90);
d.notify(100);
//d.resolve('OK');
d.reject('ERROR');
});
</script>
</body>
</html>
以上是关于html 推荐的例子。 jQuery推迟了的主要内容,如果未能解决你的问题,请参考以下文章
推迟执行连接到控制事件的 Javascript 以支持外部 jQuery 函数