promise.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const qs = require('qs');
  2. const config = {
  3. pushInner:'/api/icss/push/pushInner',
  4. calculate:'/api/icss/calc/calculate',
  5. }
  6. const imageUrlPrefix = 'http://192.168.2.241:82';
  7. const getUrlArgObject = (parm) => {
  8. var query = window.location.search;
  9. // console.log(window.location)
  10. // var query = '?'+url.split('?')[1];
  11. var args = qs.parse(query.substr(1));
  12. return args[parm];//返回对象
  13. }
  14. const post = function(url,data){
  15. return new Promise((resolve,reject)=>{
  16. $.ajax({
  17. method:'post',
  18. url:url,
  19. data:JSON.stringify(data),
  20. contentType:"application/json; charset=UTF-8",
  21. success:function(res){
  22. resolve({data:res});
  23. },
  24. error:function(error){
  25. reject(error);
  26. },
  27. });
  28. });
  29. }
  30. const throttle = function throttle(fn, threshhold) {
  31. var timeout
  32. var start = new Date;
  33. var threshhold = threshhold || 160
  34. return function () {
  35. var context = this, args = arguments, curr = new Date() - 0
  36. clearTimeout(timeout)//总是干掉事件回调
  37. if(curr - start >= threshhold){
  38. // console.log("now", curr, curr - start)//注意这里相减的结果,都差不多是160左右
  39. fn.apply(context, args) //只执行一部分方法,这些方法是在某个时间段内执行一次
  40. start = curr
  41. }else{
  42. //让方法在脱离事件后也能执行一次
  43. timeout = setTimeout(function(){
  44. fn.apply(context, args)
  45. }, threshhold);
  46. }
  47. }
  48. }
  49. export {
  50. config,
  51. post,
  52. getUrlArgObject,
  53. imageUrlPrefix,
  54. throttle
  55. }